简体   繁体   English

如何使用VB获取返回的XML并将特定元素插入SQL Server的列中?

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

I have a SQL Server database, and I need to populate it with returned xml from an api call. 我有一个SQL Server数据库,我需要用api调用返回的xml填充它。 This is the xml code that's returned(not in a file): 这是返回的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>
...

More specifically, I need to take the nodes for ID, name, animal, description, and several others, and insert them into their respectful columns in my database. 更具体地说,我需要获取ID,名称,动物,描述以及其他几个节点,并将其插入数据库中相应的列中。

And it must repeat this for each "pet" node that these are all in. 并且必须对所有这些“宠物”节点重复此操作。

Can I do this in VB.net without saving a file, just as an xml string? 我可以在VB.net中执行此操作而不保存文件(如xml字符串)吗?

Please help, I've been stuck on this for days. 请帮助,我已经坚持了好几天。

Assuming you have your XML structure in a variable (or stored procedure parameter) of type XML , you can do something like this: 假设你有一个变量类型的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

That gives you the info in those nodes as a set of rows and columns which you can easily insert into a SQL Server table. 这样就可以将这些节点中的信息作为一组行和列的形式提供,您可以轻松地将其插入到SQL Server表中。 So now you just need to find a way to call this stored procedure from your VB.NET code and pass in the XML into the @XmlData parameter 因此,现在您只需要找到一种方法即可从VB.NET代码中调用此存储过程,并将XML传递给@XmlData参数。

Here's an example of how you can extract the data for each pet from the XML using XPath and the XmlDocument class: 这是一个示例,说明如何使用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

I'm assuming you know how to save the data to your SQL database from there. 我假设您知道如何从那里将数据保存到SQL数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 VB.net 在 sql server 中执行提交/回滚 - How to do Commit/rollback in sql server using VB.net 如何使用VB作为ASPX运行SQL Server? - How can I run SQL Server using VB as a ASPX? 如何使用c#将xml数据导入Windows窗体的sql服务器数据库表中? - How do I import xml data into a sql server database table in Windows form using c#? 如何使用HTML Agility Pack for ASP.NET(vb)从HTML中删除特定元素 - How do I remove specific elements from HTML with HTML Agility Pack for ASP.NET (vb) VB Web服务代码的正确语法sql插入FOR XML AUTO,元素? - correct syntax for VB web service code sql insert FOR XML AUTO, ELEMENTS? 使用XML数据并使用C#asp.net插入sql服务器 - Take XML data and insert into sql server use c# asp.net 如何插入具有希伯来语文本的html文件,并使用filestream选项在sql server 2008中将其读回? - how do i insert html file that have hebrew text and read it back in sql server 2008 using the filestream option? 如何使用VB在ASP.NET中制作SQL插入语句 - How to make sql insert statement in asp.net using vb 如何在SQL Server中使用join插入2表? - how to insert into 2 table using join in sql server? 如何使用 ASP.NET VB 阅读此 URL XML - How do I read this URLs XML with ASP.NET VB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM