XmlDocument.SelectSingleNode 对根节点返回 null
我有几个 xml 文件,其后缀不是 .xml,而是 .component 现在我想在c#程序中处理它们,但似乎c#甚至找不到这些xml文件的根元素
var doc = new XmlDocument();
doc.Load(path); // MG: edited to Load based on comment
XmlNode root = doc.SelectSingleNode("rootNodename");
似乎根为空,我应该如何处理这个?
I have several xml files whose postfix is not .xml, but .component
Now I want to handle them in the c# program, but It seems that the c# cant even find the root element of these xml files
var doc = new XmlDocument();
doc.Load(path); // MG: edited to Load based on comment
XmlNode root = doc.SelectSingleNode("rootNodename");
It seems that the root is null, How should I cope with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
鉴于您已经解决了
Load
/LoadXml
混淆,我认为问题出在命名空间上; 你有 xml 示例吗? 使用命名空间处理 xml 会...“有趣”;-p例如:
请注意,即使您的 xml 声明了 xml 别名,您可能仍然需要为命名空间管理器重新声明它们。
Given that you've resolved the
Load
/LoadXml
confusion, I expect the issue is namespaces; do you have example xml? Handling xml with namespaces gets... "fun" ;-pFor example:
Note that even if your xml declares xml aliases, you may still need to re-declare them for your namespace-manager.
LoadXml 采用 XML 字符串,而不是文件路径。 请尝试加载。 Load 不关心文件扩展名。
以下是 Load 文档的链接:
http://msdn.microsoft.com/en-我们/library/system.xml.xmldocument.load.aspx
LoadXml takes an XML string, not a file path. Try Load instead. Load doesn't care about the file extension.
Here is a link to the documention for Load:
http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.load.aspx
我遇到这个问题尝试这个:Putting a dash in front of rootNodename
而不是这个:
XmlNode root = doc.SelectSingleNode("rootNodename");
做这个:
XmlNode root = doc.SelectSingleNode("/rootNodename");
I was having this problem try this: Putting a dash in front of rootNodename
Instead of this:
XmlNode root = doc.SelectSingleNode("rootNodename");
Do this:
XmlNode root = doc.SelectSingleNode("/rootNodename");