2013-04-02 33 views
-3

我想查詢一個XMLDocument和接收錯誤:如何訪問XmlDocument對象的Root屬性?

var query = from date in xmlDoc.Root.Elements("Serial") 

的誤差約爲Root

我完整的代碼看起來是這樣的:

private async void Button_Click_1(object sender, RoutedEventArgs e) 
{ 
    StorageFile xmlFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("Content‌​1.xml"); 
    XmlDocument xmlDoc; 
    xmlDoc = await XmlDocument.LoadFromFileAsync(xmlFile); 
    System.Xml.Linq.XDocument duc = System.Xml.Linq.XDocument.Parse(xmlDoc.GetXml()); 
    var query = from Date in xmlDoc.Root.Elements("Serial") 
     where Date.Attribute("No").Value == "1"; 
} 

我如何可以訪問XmlDocument對象的Root財產?

+0

請更精確 –

+0

XML解析需要:使用的System.Xml – Gerry

+0

我使用的System.Xml還是其示值誤差 – maha

回答

3

一定要在文件的頂部有這些:

using System.Linq; 
using System.Xml; 
using System.Xml.Linq; 
using System.IO; 

如果您正在編寫一個Windows 8的「沉浸式」的應用程序,補充一點:

using Windows.Data.Xml.Dom; 
+0

我用上述所有提到的命名空間,但它仍然顯示錯誤在根詞 – maha

+0

你是否添加System.Xml作爲參考大會資源管理器下? –

+0

感謝大家..我的問題得到解決..非常感謝你:) :) :) – maha

4

xmlDoc對象是鍵入XmlDocumentXmlDocument does not have a property called Root

訪問根的XmlDocument的,使用DocumentElement財產

XmlElement root = xmlDoc.DocumentElement; 

應當指出的是DocumentElementXmlElement類型,XmlElement不含有一種叫Elements屬性,所以你需要尋找如果你選擇堅持使用XmlDocument

但是在你的情況下,你可能會得到XmlDocumentXDocument類混在一起,其中確實包含XElement類型的名爲Root的財產,而XElement包含Elements財產。

因此,或者用XDocument代替XmlDocument,或者重寫您的linq查詢以使用XmlDocument語法。

+0

這是正確的答案。 –