2

我有一个 SQL Server 数据库,我需要使用从 api 调用返回的 xml 填充它。这是返回的 xml 代码(不在文件中):

<petfinder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xsi:noNamespaceSchemaLocation="http://api.petfinder.com/schemas/0.9/petfinder.xsd">
<header>
 <version>0.1</version>
 <timestamp>2013-04-08T14:52:23Z</timestamp>
 <status>
  <code>100</code>
  <message/>
 </status>
</header>
<lastOffset>25</lastOffset>
 <pets>
  <pet>
   <id>18589607</id>
   <shelterId>OK98</shelterId>
   <shelterPetId>11C-0015</shelterPetId>
   <name>Sam</name>
   <animal>Cat</animal>
   <breeds>
    <breed>Domestic Short Hair</breed>
    <breed>Tabby</breed>
   </breeds>
   <mix>yes</mix>
   <age>Adult</age>
   <sex>M</sex>
   <size>XL</size>
   <options>
    <option>altered</option>
    <option>hasShots</option>
    <option>housebroken</option>
   </options>
  <description>
   <![CDATA[
    <div>This guy loves the camera. Look at him pose and show off! Sam is about 5 years old and is a cream Tabby. He is good with other cats and is house trained. He has turquoise eyes and is a sweet sweet cat. Sam loves to be the right hand man and assist you on any task you may have. Sammy is not the type of cat that likes to be held but will sit right next to you for some rubbing and head butting. Our adoption fee is $100 for dogs and $75 for cats. This adoption fee includes the spay or neutering and rabies shot. </div>
   ]]>
  </description>
  <lastUpdate>2012-07-24T14:50:17Z</lastUpdate>
  <status>A</status>
  <media>
   <photos>
    <photo id="1" size="x">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-1-x.jpg
    </photo>
    <photo id="1" size="fpm">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-1-fpm.jpg
    </photo>
    <photo id="1" size="pn">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-1-pn.jpg
    </photo>
    <photo id="1" size="pnt">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-1-pnt.jpg
    </photo>
    <photo id="1" size="t">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-1-t.jpg
    </photo>
    <photo id="2" size="x">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-2-x.jpg
    </photo>
    <photo id="2" size="fpm">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-2-fpm.jpg
    </photo>
    <photo id="2" size="pn">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-2-pn.jpg
    </photo>
    <photo id="2" size="pnt">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-2-pnt.jpg
    </photo>
    <photo id="2" size="t">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-2-t.jpg
    </photo>
    <photo id="3" size="x">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-3-x.jpg
    </photo>
    <photo id="3" size="fpm">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-3-fpm.jpg
    </photo>
    <photo id="3" size="pn">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-3-pn.jpg
    </photo>
    <photo id="3" size="pnt">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-3-pnt.jpg
    </photo>
    <photo id="3" size="t">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-3-t.jpg
    </photo>
   </photos>
  </media>
  <contact>
   <address1>714 Martin Luther King Jr Ave</address1>
   <address2/>
   <city>Duncan</city>
   <state>OK</state>
   <zip>73533</zip>
   <phone/>
   <fax/>
   <email/>
  </contact>
 </pet>
...

更具体地说,我需要获取 ID、名称、动物、描述和其他几个节点,并将它们插入到我数据库中相应的列中。

它必须对所有这些都在其中的每个“宠物”节点重复此操作。

我可以在 VB.net 中执行此操作而不保存文件,就像 xml 字符串一样吗?

请帮忙,我已经坚持了好几天了。

4

2 回答 2

0

假设您的 XML 结构位于 type 的变量(或存储过程参数)中XML,您可以执行以下操作:

CREATE PROCEDURE dbo.InsertXmlData 
     @XmlData XML
AS BEGIN
   INSERT INTO dbo.YourTable(ID, PetName, Animal, Description)
      SELECT
         ID = Pet.value('(id)[1]', 'int'),
         PetName = Pet.value('(name)[1]', 'varchar(50)'),
         Animal = Pet.value('(animal)[1]', 'varchar(50)'),
         [Description] = Pet.value('(description)[1]', 'varchar(500)')
      FROM 
         @XmlData.nodes('/petfinder/pets/pet') AS xTBL(Pet)
END

这为您提供了这些节点中的信息作为一组行和列,您可以轻松地将其插入到 SQL Server 表中。所以现在你只需要找到一种方法来从你的 VB.NET 代码中调用这个存储过程并将 XML 传递给@XmlData参数

于 2013-04-08T15:28:56.607 回答
0

下面是一个示例,说明如何使用 XPath 和XmlDocument类从 XML 中提取每只宠物的数据:

Dim doc As XmlDocument = New XmlDocument()
doc.LoadXml(xmlString)
For Each pet As XmlNode In doc.SelectNodes("/petfinder/pets/pet")
    Dim id As String = pet.SelectSingleNode("id").InnerText
    Dim name As String = pet.SelectSingleNode("name").InnerText
    ' ...
Next

我假设您知道如何将数据从那里保存到您的 SQL 数据库中。

于 2013-04-08T15:45:44.157 回答