簡體   English   中英

使用VB.NET將xmlNodeList讀入數據集

[英]Reading xmlNodeList into a Dataset using VB.NET

更新:

我想動態地將返回的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>

我一直在使用標准格式來訪問我們的Web服務並返回特定字段。 我需要修改它以將整個XML提要轉儲到VB.NET中的內部數據集中。 此Web服務在我的Web References部分中預定義。 我在下面的代碼中取得了成功,但是經過大量的谷歌搜索和測試后,我無法找到一種方法來動態地將所有列加載到數據集中。 我可以使用下面的格式獲取幫助加載到數據集中嗎?

    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

如果您想知道“runsql”是我們的供應商提供給我們的Web服務末尾的參數。 (webservices.asmx?OP = runsql)

基本上,您只需要使用DataSet.ReadXML加載XMLDocument 這是一種方式:

' 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數據粘合在一起。 無需指定表名或列名,它會自行解析它們。 在您的情況下,您可以忽略DataSet除“runsql”之外的任何其他表。 我在編寫測試代碼后粘貼了你的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

適用於My Machine TM

暫無
暫無

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

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