XPathNavigator 帮助
这是我正在使用的 XML 示例(可从任何 wiki 的 Special:Export/SomePage 结果中检索):
<mediawiki xmlns="http://www.mediawiki.org/xml/export-0.4/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.4/ http://www.mediawiki.org/xml/export-0.4.xsd" version="0.4" xml:lang="en">
<siteinfo>
<sitename>Wikipedia</sitename>
<base>http://en.wikipedia.org/wiki/Main_Page</base>
<generator>MediaWiki 1.16wmf4</generator>
<case>first-letter</case>
<namespaces>
<namespace key="-2" case="first-letter">Media</namespace>
<namespace key="-1" case="first-letter">Special</namespace>
<namespace key="0" case="first-letter" />
...
</namespaces>
</siteinfo>
</mediawiki>
我已经尝试了所有我能想到的直接“跳转”到 siteinfo 节点并迭代结果的方法,但没有任何效果除非我从根向下手动导航每个子节点。我已经尝试了各种 .Move* 和 .Select* 方法的一百万种变体,看起来我只是在用头撞墙,但我当前的变体看起来像这样:
StringReader strr = new StringReader(_rawData);
XPathDocument xd = new XPathDocument(XmlReader.Create(strr, Bot.XmlReaderSettings));
XPathNavigator xn = xd.CreateNavigator();
XPathNodeIterator xni = xn.Select("/mediawiki/siteinfo");
foreach (XPathNavigator nav in xni)
Console.WriteLine(nav.LocalName);
这不会返回任何结果。我做错了什么?
Here's a sample of the XML I'm working with (retrievable from any wiki's Special:Export/SomePage results):
<mediawiki xmlns="http://www.mediawiki.org/xml/export-0.4/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.4/ http://www.mediawiki.org/xml/export-0.4.xsd" version="0.4" xml:lang="en">
<siteinfo>
<sitename>Wikipedia</sitename>
<base>http://en.wikipedia.org/wiki/Main_Page</base>
<generator>MediaWiki 1.16wmf4</generator>
<case>first-letter</case>
<namespaces>
<namespace key="-2" case="first-letter">Media</namespace>
<namespace key="-1" case="first-letter">Special</namespace>
<namespace key="0" case="first-letter" />
...
</namespaces>
</siteinfo>
</mediawiki>
I've tried everything I can think of to "jump" directly to the siteinfo node and iterate the results, and nothing works unless I navigate manually through each child node from the root down. I've tried a million variations of the various .Move* and .Select* methods and it seems I'm just bashing my head against a wall, but my current variant looks like this:
StringReader strr = new StringReader(_rawData);
XPathDocument xd = new XPathDocument(XmlReader.Create(strr, Bot.XmlReaderSettings));
XPathNavigator xn = xd.CreateNavigator();
XPathNodeIterator xni = xn.Select("/mediawiki/siteinfo");
foreach (XPathNavigator nav in xni)
Console.WriteLine(nav.LocalName);
This returns no results. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
欢迎使用 XML 命名空间。您需要在前缀和根元素的
xmlns
之间创建映射。例如,此代码对我有用:如果您有兴趣,请进一步阅读: MSDN :XML 命名空间及其如何影响 XPath 和 XSLT。
Welcome to XML namespaces. You need to create a mapping between a prefix and the
xmlns
of the root element. For example, this code worked for me:Further reading, in case you are interested: MSDN: XML Namespaces and How They Affect XPath and XSLT.