[英]XmlDocument SelectSingleNode
在 Stack Overflow 上有一個文檔解釋了 XmlDocument 的使用以及如何 select 一個節點。
C# XmlDocument SelectSingleNode 無屬性
提供的代碼是我正在使用的代碼,如下所示。
XmlDocument doc = new XmlDocument();
doc.Load("C:\\FileXml.xml")
string Version = doc.DocumentElement.SelectSingleNode("/Version").InnerText;
Console.Write(Version); //I want to see 3
Xml 文件“完整”如下所示。
<CharacterObject xmlns="http://www.w3.org/2005/Atom">
<Version>3</Version>
<Path>C:\\FilePath\FileName.xml</Path>
</CharacterObject>
我收到的錯誤是上面的 SelectSingleNode 返回 null。 當我搜索 CharacterObject 時,它也返回了 null。 無論 XML 節點是什么,我搜索 function SelectSingleNode 返回 null。 這意味着我必須錯誤地使用 SingleSelectNode 只是不確定如何。
我希望 SelectSingleNode 返回節點,以便 InnerText 將返回 XML 節點中的版本信息。 我只是在使用從節點讀取信息時遇到問題。
根據XmlDocument.DocumentElement
上的文檔- 它是一個根 xml 元素。 所以在你的情況下,它已經是CharacterObject
了。 當您為其調用.SelectSingleNode('/CharacterObject')
時-您正在請求根CharacterObject
內的CharacterObject
元素-根本不存在。
您可以簡單地使用XmlDocument.DocumentElement.InnerText
來實現您想要的結果。
這個特殊的問題有一個解決方案。 這可能是由於 XML 根節點本身的命名空間屬性。 消除此屬性解決了我的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.