2009-12-16 52 views
0

我在解析從Web服務返回的xml時遇到問題,因爲它實際上只是一個字符串。 Web服務不包含任何提交請求的方法,也不包含處理響應的對象,因此我只是將xml作爲字符串獲取並嘗試將其解析爲我創建的對象中的屬性。帶有字符串或XmlDocument的XPath

我搞砸了XPath,但我無法弄清楚如何在Xpath中使用字符串或XmlDocument對象。我沒有一個實際的xml文件,只是一個我用來創建XmlDocument對象的字符串。

private void SetProperties(string _xml) 
    { 
     XmlDocument _doc = new XmlDocument(); 
     _doc.LoadXml(_xml); 
    } 

有關如何使用XPath查詢XmlDocument對象的任何想法?

+1

如果你有/可以使用.NET 3.0+使用LINQ到XML和的XDocument代替的XmlDocument - 給你更多的遊戲方式。 – Murph 2009-12-16 21:57:39

回答

0

您是否嘗試過調用XmlDocument對象上的各種方法?例如,SelectSingleNode方法接受一個xpath字符串並返回一個xmlNode。

此外,檢查該網站的其他信息:http://www.w3schools.com/

0

您可以創建一個新的XPathExpression對象,你那麼做一個選擇對與它的XDOC。添加到您開始使用的XDocument加載代碼:

XmlDocument _doc = new XmlDocument(); 
_doc.LoadXml(_xml); 

XPathNavigator navigator = _doc.CreateNavigator(); 
XPathExpression expression = navigator.Compile("/foo/bar"); 
XPathNodeIterator iterator = navigator.Select(expression); 

while (iterator.HasNext()) { 
    //Do Something With iterator.Current.Value; 
} 
+0

現在,當我嘗試實例化XPathNodeIterator時出現此錯誤:「名稱空間管理器或XsltContext需要,此查詢具有前綴,變量或用戶定義的函數。」我在網上找不到任何幫助。有什麼建議麼? – Barryman9000 2009-12-17 00:22:41