使用VB.NET将xmlNodeList读入数据集

时间:2014-11-13 17:32:25

标签: asp.net xml vb.net web-services

更新:

我想动态地将返回的XML转储到数据集中,而不必写出列名。返回的数据格式非常简单,列名,数据,然后关闭列的名称。像这样:

<runsql>
    <cst_id>0005675667</cst_id>
    <ind_last_name>Abe</ind_last_name>
    <ind_first_name>Adam</ind_first_name>
    <cst_ixo_title_dn/>
    <cst_org_name_dn>Acme University</cst_org_name_dn>
    <cst_eml_address_dn>Adam@acmeu.edu</cst_eml_address_dn>
</runsql>

我一直使用标准格式访问我们的网络服务并返回特定字段。我需要修改它以将整个XML提要转储到VB.NET中的内部数据集中。此Web服务在我的Web References部分中预定义。我在下面的代码中取得了成功,但是经过大量的Google搜索和测试后,我无法找到一种方法来动态地将所有列加载到数据集中。我可以使用以下格式获取帮助以加载到数据集吗?

    Dim proxy As New myWS.netFORUMXMLWebServices
    Dim strInSQL As String
    Dim strOutXML2 As XmlDocument
    strOutXML2 = New XmlDocument
    Dim oNode2 As XmlNode
    Dim oResultsNode2 As XmlNode

    strInSQL = "SELECT cst_eml_address_dn FROM WebServicesTable"

    strOutXML2.LoadXml("<myResults></myResults>")
    oResultsNode2 = proxy.runsql(strInSQL)
    Dim xmlNewDoc As XmlDocument
    xmlNewDoc = New XmlDocument
    xmlNewDoc.LoadXml(oResultsNode2.OuterXml)
    strOutXML2.DocumentElement.AppendChild(strOutXML2.ImportNode(xmlNewDoc.DocumentElement, True))
    Dim oResultsNodeList2 As XmlNodeList
    oResultsNodeList2 = xmlNewDoc.SelectNodes("//runsql")

    For Each oNode2 In oResultsNodeList2

        returnedEmail = oNode2.SelectSingleNode("cst_eml_address_dn").InnerText

    Next

如果你想知道&#34; runsql&#34;是我们的供应商提供给我们的Web服务末尾的参数。 (webservices.asmx?op = runsql)

1 个答案:

答案 0 :(得分:3)

基本上,您只需使用XMLDocument加载DataSet.ReadXML即可。这是一种方式:

' build xmlDoc to stand in for [proxy.runsql] return
Dim xdoc As New XmlDocument

' xml literal for the data
Dim x = <Document>
            <Employee>
                <Name>Ziggy Foobar</Name>
                <HireDate>2/11/2010</HireDate>
            </Employee>

            <Elements>
                <Name>Helium</Name>
                <Symbol>He</Symbol>
            </Elements>

            <Employee>
                <Name>Zoey Foobaz</Name>
                <HireDate>2/11/2013</HireDate>
            </Employee>

            <runsql>
                <cst_id>0005675667</cst_id>
                <ind_last_name>Abe</ind_last_name>
                <ind_first_name>Adam</ind_first_name>
                <cst_ixo_title_dn/>
                <cst_org_name_dn>Acme University</cst_org_name_dn>
                <cst_eml_address_dn>Adam@acmeu.edu</cst_eml_address_dn>
            </runsql>
        </Document>

' load litersl to XmlDocument
xdoc.LoadXml(x.ToString)            

'***** xdoc is now a stand in for the return from [proxy.runsql]

Dim ds As New DataSet

' load xdoc to dataset via node reader
Using xnr As New XmlNodeReader(xdoc)
    ds.ReadXml(xnr)           ' this is what you want
End Using                     ' dispose of node reader

Dim n As Integer                             ' test/view DS result
' verify: 2 tables?
For Each t As DataTable In ds.Tables
    Console.WriteLine("Table: " & t.TableName)

    ' we know there are 2, just verifying
    Console.WriteLine("Column Names: {0}, {1}", t.Columns(0).ColumnName,
                      t.Columns(1).ColumnName)

    n = 1
    For Each r As DataRow In t.Rows
        ' demo there only 2 cols
        Console.WriteLine("[Item {0}]:  {1}, {2}", n.ToString,
                          r(0).ToString, r(1).ToString)
        n += 1
    Next
    Console.WriteLine()

Next

最终结果是DS中的3个表,其中不连续的Employee数据粘合在一起。无需指定表名或列名,它会自行解析它们。在您的情况下,您可以忽略除{runsql}之外的DataSet中的任何其他表。我在编写测试代码后粘贴了你的runsql块,因此一些“只有2列”的注释是错误的。输出:

Table: Employee
Column Names: Name, HireDate
[Item 1]:  Ziggy Foobar, 2/11/2010
[Item 2]:  Zoey Foobaz, 2/11/2013

Table: Elements
Column Names: Name, Symbol
[Item 1]:  Helium, He

Table: runsql
Column Names: cst_id, ind_last_name
[Item 1]:  0005675667, Abe

适用于我的机器 TM