C#的XmlDocument的SelectNodes [英] C# XmlDocument SelectNodes

查看:103
本文介绍了C#的XmlDocument的SelectNodes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个根元素的XML文档,两个子元素,诊断和结果。在'结果'元素则有一个名为结果的元素任意数量的

I have an xml document with a root element, two child elements, 'diagnostic' and 'results'. The 'results' element then has an arbitrary number of elements with the name 'result'

在这个被加载到XmlDocument它易于导航的结构和看到这正是事物如何运作。我可以写挑出所有的结果元素的递归函数。该XmlDocument.SelectNodes(//结果)发现的一个节点没有问题。

When this is loaded into an XmlDocument it is easy to navigate the structure and see that this is exactly how things operate. I can write a recursive function that picks out all the "result" elements. The XmlDocument.SelectNodes("//results") finds a node no problem.

不过,
* XmlDocument.SelectNodes(//结果/结果),觉得没有什么。

* XmlDocument.SelectNodes( //结果),觉得没有什么。

However, * XmlDocument.SelectNodes("//results/result") finds nothing.
* XmlDocument.SelectNodes("//result") finds nothing.

我跟一个同事和他在XmlDocument.SelectNodes使用XPath有悲伤。任何人都遇到这样的问题?任何解决方案

I've talked to a co-worker and he's had grief using Xpath in XmlDocument.SelectNodes. Anyone else run into this kind of problem? Any solutions?

XML文件:<?/ p>

XML FILE:

<?xml version="1.0" encoding="UTF-8"?>
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="10" yahoo:created="2009-08-07T10:19:59Z" yahoo:lang="en-US" yahoo:updated="2009-08-07T10:19:59Z" yahoo:uri="http://query.yahooapis.com/v1/yql?q=select+*+from+search.news+where+query%3D%22Tanzania%22">
    <diagnostics>
        <publiclyCallable>true</publiclyCallable>
        <url execution-time="47"><![CDATA[http://boss.yahooapis.com/ysearch/news/v1/Tanzania?format=xml&start=0&count=10]]></url>
        <user-time>49</user-time>
        <service-time>47</service-time>
        <build-version>2579</build-version>
    </diagnostics>
    <results>
        <result xmlns="http://www.inktomi.com/">
            <abstract>Kakungulu Cup winners SC Villa face Tanzania’s Simba SC this afternoon at the National stadium in Dar es salaam. "We had a very tiresome journey. The road was so bad and the road blocks were so many. However, we finally reached but the boys were so tired," said Kato.</abstract>
            <clickurl>http://lrd.yahooapis.com/_ylc=X3oDMTQ4cXAxcnRoBF9TAzIwMjMxNTI3MDIEYXBwaWQDb0pfTWdwbklrWW5CMWhTZnFUZEd5TkouTXNxZlNMQmkEY2xpZW50A2Jvc3MEc2VydmljZQNCT1NTBHNsawN0aXRsZQRzcmNwdmlkA21VVGlta2dlQXUzeEYuM0xGQkQzR1pUU1FIS0dORXA4cUk4QUJJX1U-/SIG=12vhpskdd/**http%3A//www.monitor.co.ug/artman/publish/sports/SC_Villa_face_Simba_in_Tanzania_89289.shtml</clickurl>
            <date>2009/08/07</date>
            <language>english</language>
            <source>The Monitor</source>
            <sourceurl>http://www.monitor.co.ug/</sourceurl>
            <time>20:22:32</time>
            <title>SC Villa face Simba in Tanzania</title>
            <url>http://www.monitor.co.ug/artman/publish/sports/SC_Villa_face_Simba_in_Tanzania_89289.shtml</url>
        </result>



XPATH

XPATH

doc.SelectNodes( //结果),不产生任何安打

doc.SelectNodes("//result") produces no hits.

推荐答案

罗布和马克的回答很可能会在正确的方向 - 的XmlDocument +命名空间+ XPath的可以是一个有点疼痛。

Rob and Marc's answers are probably going in the right direction - XmlDocument + namespaces + XPath can be a bit of a pain.

如果你能使用.NET 3.5的,我建议你使用LINQ到XML来代替。这将使它的真正的容易:

If you're able to use .NET 3.5, I suggest you use LINQ to XML instead. That would make it really easy:

XDocument doc = XDocument.Load("foo.xml");
XNamespace ns = "bar";
var results = doc.Descendants(ns + "result");

foreach (var result in results)
{
    ...
}

基本上的LINQ to XML是几乎每个方式优越的API,以我的经验:)(我相信有一些功能,它缺少的,但如果你有机会到.NET 3.5这绝对至少价值尝试。)

Basically LINQ to XML is a superior API in almost every way, in my experience :) (I believe there are some capabilities it's missing, but if you have access to .NET 3.5 it's definitely worth at least trying.)

这篇关于C#的XmlDocument的SelectNodes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆