2012-11-23 85 views
1

我正在嘗試從傳遞給我的方法的文件路徑中讀取XML節點。VB - 使用XPath讀取XML的問題

Public Function ReadXMLForIsite(xmlFileName As String) 
    Dim IsitePath As String 
    Dim doc As New XPathDocument(xmlFileName) 
    Dim nav As XPathNavigator 
    Dim iter As XPathNodeIterator 

    nav = doc.CreateNavigator 
    iter = nav.Select("GovTalkMessage/Header") 'Node name 
    'Loop through the records in that node 
    While iter.MoveNext 
     Dim lstNav As XPathNavigator 
     'Get the data we need from the node 
     Dim iterNews As XPathNodeIterator 
     lstNav = iter.Current 
     iterNews = lstNav.SelectDescendants(XPathNodeType.Element, False) 
     'Loop through the child nodes 
     While iterNews.MoveNext 
      Debug.WriteLine(iterNews.Current.Name & ": " & iterNews.Current.Value) 
     End While 
    End While 

    Return IsitePath 
End Function 

每次我運行此方法(即使使用不同的節點名稱)變量「ITER」指出「調試器顯示代理是一種類型的,不能用作表達式」。這發生在while語句之前,因此不會進入。任何幫助將不勝感激。謝謝!

+1

雅各布和盧克...嗯! – Virus

回答

1

嘗試將您的Xml加載到XDocument中。

Dim xdoc As XDocument = XDocument.Load("X:\Jacob.Freeman\RTI\TestXML\0001R.xml") 
Dim headerNode = xdoc.Descendants.First(Function(x) x.Name.LocalName = "Header") 
For Each desc In headerNode.Descendants() 
    Console.WriteLine(desc.Name.LocalName + ":" + desc.Value) 
Next 
Console.ReadLine()