【发布时间】:2011-09-20 00:06:10
【问题描述】:
我在解析带有命名空间的 xml 文件时遇到了一些问题
xml文件有这样一行
<my:include href="include/myfile.xml"/>
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(file);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("my", "http://www.w3.org/2001/xinclude");
XmlNodeList includeNodeList = xmlDoc.SelectNodes(@"/root/my:include", nsmgr);
我习惯做这样的事情,但这不是我认为应该如何阅读的内容.. node["href"] 为空,无论我如何更改似乎都无法获得
foreach (XmlNode node in includeNodeList)
{
if (node["href"] != null)
{
// Save node["href"].Value here
}
}
如果我在调试器中停止它,我可以看到节点在 Outertext 中有我想要的信息。 ..我可以保存外部文本并以这种方式解析它,但我知道必须有一些简单的东西我忽略了。谁能告诉我我需要做什么才能获得href值。
【问题讨论】:
-
你能发布更多的 XML,比如声明
xmlns的地方吗? -
你能显示源 XML 吗?使用根和命名空间定义。
标签: c# parsing namespaces xmldocument