XPATH 帮助:使用 XPathNodeIterator 在命名空间中查找 XML 节点
我在寻找正确的 XPATH 语法来获取我想要的 XML 文件节点时遇到问题。其一,XML 中没有 NameSPace,因此我必须在代码中添加一个。我认为这会影响我的 XPATH。
我有一个如下所示的 XML 文件:
<configuration xmlns="http://schemas.microsoft.com/support/2003/02/config">
<name>Content</name>
<description>Desc</description>
<lastModifiedBy>Me</lastModifiedBy>
<lastModifiedDate>2011-04-18T14:05:00</lastModifiedDate>
<section name="MaintenanceNotices">
<key name="MaintenanceNote1" value="The Title Value" />
<link id="1234" type="5" href="" target="_self" name="MaintenanceNote1a">
<description>Description</description>
</link>
</section>
</configuration>
因此,对于 XPATH,如果我想获取“CONFIGURATION”元素中的“NAme”NODE 值,我假设我会使用此 XPATH:
/configuration/name
但有时我需要将 ns: 附加到it:
/ns:configuration/ns:name
然后我可以像这样找到元素值:
while (xmlNodeIterator.MoveNext()) {
result += xmlNodeIterator.Current.SelectSingleNode("name", nsmgr).ToString();
}
但这对我来说根本不起作用。无论我尝试什么 xpath,它都不会在 XML 中找到任何值。这是我的代码:
private string GetXML()
{
string result = string.Empty;
string fileName = "Content.xml";
string filePath = "C:\\Content\\{0}";
XPathDocument xdoc = new XPathDocument(string.Format(filePath, fileName));
var nav = xdoc.CreateNavigator();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
nsmgr.AddNamespace("ns", "http://schemas.microsoft.com/taxonomy/2003/1");
nsmgr.AddNamespace("gds", "http://support.microsoft.com/common/schemas/gdsPage/1/");
string sectionName = "MaintenanceNotices";
string xpath = "/configuration";
// section[name={0}]"; ///key[name=MaintenanceNote1]/";
XPathNodeIterator xmlNodeIterator = nav.Select(string.Format(xpath, sectionName), nsmgr);
while (xmlNodeIterator.MoveNext())
{
result += xmlNodeIterator.Current.SelectSingleNode("name", nsmgr).ToString();
}
return result;
}
您能看到任何问题,或者对我的 Xpath 语法提出建议吗?
谢谢。
I am having problems finding the proper XPATH syntax to get the nodes I want with this XML file. For one, there is no NameSPace in the XML so I have to add one in code. I think this effects my XPATH.
I have an XML file that looks like this:
<configuration xmlns="http://schemas.microsoft.com/support/2003/02/config">
<name>Content</name>
<description>Desc</description>
<lastModifiedBy>Me</lastModifiedBy>
<lastModifiedDate>2011-04-18T14:05:00</lastModifiedDate>
<section name="MaintenanceNotices">
<key name="MaintenanceNote1" value="The Title Value" />
<link id="1234" type="5" href="" target="_self" name="MaintenanceNote1a">
<description>Description</description>
</link>
</section>
</configuration>
So, for the XPATH if I want to get the "NAme" NODE value in the "CONFIGURATION" element, I assume I would use this XPATH:
/configuration/name
but sometimes I need to append ns: to it:
/ns:configuration/ns:name
And then I can find the element value like so:
while (xmlNodeIterator.MoveNext()) {
result += xmlNodeIterator.Current.SelectSingleNode("name", nsmgr).ToString();
}
But this is not working for me at all. It wont find any values in the XML no matter what xpath i try. Here is my code:
private string GetXML()
{
string result = string.Empty;
string fileName = "Content.xml";
string filePath = "C:\\Content\\{0}";
XPathDocument xdoc = new XPathDocument(string.Format(filePath, fileName));
var nav = xdoc.CreateNavigator();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
nsmgr.AddNamespace("ns", "http://schemas.microsoft.com/taxonomy/2003/1");
nsmgr.AddNamespace("gds", "http://support.microsoft.com/common/schemas/gdsPage/1/");
string sectionName = "MaintenanceNotices";
string xpath = "/configuration";
// section[name={0}]"; ///key[name=MaintenanceNote1]/";
XPathNodeIterator xmlNodeIterator = nav.Select(string.Format(xpath, sectionName), nsmgr);
while (xmlNodeIterator.MoveNext())
{
result += xmlNodeIterator.Current.SelectSingleNode("name", nsmgr).ToString();
}
return result;
}
Can you see any problems, or offer a suggestion on my Xpath Syntax?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没有发现您的 XPath 有任何问题,只是您从未注册文档节点所在的命名空间:
看起来您已经知道如何用您的语言执行此操作(使用
AddNamespace
),所以也许这只是一个疏忽。如果您需要更多信息,这应该会有所帮助:I don't see anything wrong with your XPath, except that you never register the namespace that your document's nodes are in:
It looks like you already know how to do that in your language (using
AddNamespace
), so maybe it's just an oversight. If you need more information, this should help:正如@lwburk所说,您需要注册名称空间。
选择语句应该是:
As @lwburk said, you need to register the namespace.
The select statement should be: