在堆栈溢出上,有一个文档解释了XmlDocument的使用以及如何选择节点。
无属性的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 3Xml文件如下所示:“完整”。
<CharacterObject xmlns="http://www.w3.org/2005/Atom">
<Version>3</Version>
<Path>C:\\FilePath\FileName.xml</Path>
</CharacterObject>我收到的错误是上面的SelectSingleNode返回null。在搜索CharacterObject时,它也返回null。无论我搜索哪个XML节点,SelectSingleNode都返回null。这意味着我一定是错误地使用了SingleSelectNode,只是不确定如何使用。
我希望SelectSingleNode返回节点,以便InnerText将返回XML中的版本信息。我只是在使用从节点读取信息方面遇到了问题。
发布于 2022-09-13 12:21:59
根据文档 on XmlDocument.DocumentElement --它是根xml元素。所以在你的例子中,它已经是CharacterObject了。为此调用.SelectSingleNode('/CharacterObject')时--在根CharacterObject中请求一个CharacterObject元素--根本不存在。
您可以简单地使用XmlDocument.DocumentElement.InnerText来实现您想要的结果。
发布于 2022-09-13 14:32:41
这个特殊的问题有一个解决办法。这可能是由于XML根节点本身的命名空间属性造成的。消除这个属性解决了我的问题。
https://stackoverflow.com/questions/73702854
复制相似问题