簡體   English   中英

如何使用VB獲取返回的XML並將特定元素插入SQL Server的列中?

[英]How do I take returned XML and insert specific elements into columns in SQL Server using VB?

我有一個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字符串)嗎?

請幫助,我已經堅持了好幾天。

假設你有一個變量類型的XML的結構(或存儲過程參數) 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參數。

這是一個示例,說明如何使用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數據庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM