Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Decent子级的Xpath_C#_Xml - Fatal编程技术网

C# Decent子级的Xpath

C# Decent子级的Xpath,c#,xml,C#,Xml,我有一个响应XML,在其中我试图找到Entry标记的id,但是任何组合都会产生null <?xml version="1.0" encoding="UTF-8"?> <feed xmlns="http://www.w3.org/2005/Atom"> <status feed="http://feeds.bbci.co.uk/news/rss.xml?edition=int" xmlns="http://superfeedr.com/xmpp-pubsub-ext"

我有一个响应XML,在其中我试图找到Entry标记的id,但是任何组合都会产生null

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<status feed="http://feeds.bbci.co.uk/news/rss.xml?edition=int" xmlns="http://superfeedr.com/xmpp-pubsub-ext">
<http code="200">Fetched (ring) 200 242 and parsed 2/42 entries</http>
<next_fetch>1970-01-18T20:24:54.289Z</next_fetch>
<entries_count_since_last_maintenance>35</entries_count_since_last_maintenance>
<velocity>65.3</velocity>
<popularity>3.713318235496007</popularity>
<generated_ids>true</generated_ids>
<title>BBC News - Home</title>
<period>242</period>
<last_fetch>1970-01-18T20:24:54.045Z</last_fetch>
<last_parse>1970-01-18T20:24:54.045Z</last_parse>
<last_maintenance_at>1970-01-18T20:24:07.350Z</last_maintenance_at>
</status>
<link title="BBC News - Home" rel="alternate" href="https://www.bbc.co.uk/news/" type="text/html"/>
<link title="BBC News - Home" rel="image" href="https://news.bbcimg.co.uk/nol/shared/img/bbc_news_120x60.gif" type="image/gif"/>
<title>BBC News - Home</title>
<updated>2018-11-15T14:59:15.000Z</updated>
<id>bbc-news-home-2018-11-15-14</id>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:geo="http://www.georss.org/georss" xmlns:as="http://activitystrea.ms/spec/1.0/" xmlns:sf="http://superfeedr.com/xmpp-pubsub-ext" xml:lang="en">
<id>https://www.bbc.co.uk/news/world-us-canada-46225486</id>
<published>2018-11-15T14:44:37.000Z</published>
<updated>2018-11-15T14:44:37.000Z</updated>
<title>Trump attacks Mueller's Russia inquiry as 'absolutely nuts'</title>
<summary type="text">The US president says the Russia inquiry is a "total mess" and calls investigators "a disgrace".</summary>
<link title="Trump attacks Mueller's Russia inquiry as 'absolutely nuts'" rel="alternate" href="https://www.bbc.co.uk/news/world-us-canada-46225486" type="text/html" xml:lang="en"/>
<link title="Trump attacks Mueller's Russia inquiry as 'absolutely nuts'" rel="thumbnail" href="http://c.files.bbci.co.uk/E64B/production/_104355985_gettyimages-1060191940.jpg" type="image/jpeg" xml:lang="en"/>
</entry>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:geo="http://www.georss.org/georss" xmlns:as="http://activitystrea.ms/spec/1.0/" xmlns:sf="http://superfeedr.com/xmpp-pubsub-ext" xml:lang="en">
<id>https://www.bbc.co.uk/news/world-africa-46221238</id>
<published>2018-11-15T14:35:47.000Z</published>
<updated>2018-11-15T14:35:47.000Z</updated>
<title>Ethiopia arrests former deputy spy chief Yared Zerihun</title>
<summary type="text">Prime Minister Abiy Ahmed promised to combat corruption and rights abuses when he took office.</summary>
<link title="Ethiopia arrests former deputy spy chief Yared Zerihun" rel="alternate" href="https://www.bbc.co.uk/news/world-africa-46221238" type="text/html" xml:lang="en"/>
<link title="Ethiopia arrests former deputy spy chief Yared Zerihun" rel="thumbnail" href="http://c.files.bbci.co.uk/52E9/production/_104352212_872d41ed-8ac9-4b7b-abfc-b4d898a71670.jpg" type="image/jpeg" xml:lang="en"/>
</entry>
</feed>

获取(环)200 242和解析的2/42项
1970-01-18T20:24:54.289Z
35
65.3
3.713318235496007
真的
英国广播公司新闻-主页
242
1970-01-18T20:24:54.045Z
1970-01-18T20:24:54.045Z
1970-01-18T20:24:07.350Z
英国广播公司新闻-主页
2018-11-15T14:59:15.000Z
bbc-news-home-2018-11-15-14
https://www.bbc.co.uk/news/world-us-canada-46225486
2018-11-15T14:44:37.000Z
2018-11-15T14:44:37.000Z
特朗普攻击穆勒的俄罗斯调查“绝对是疯子”
美国总统说,俄罗斯的调查“一团糟”,并称调查人员是“耻辱”。
https://www.bbc.co.uk/news/world-africa-46221238
2018-11-15T14:35:47.000Z
2018-11-15T14:35:47.000Z
埃塞俄比亚逮捕前副间谍局长亚雷德·泽里洪
艾比·艾哈迈德总理上任后承诺打击腐败和侵犯人权行为。
要获取id,这些是我尝试的组合

  • “/feed/entry/id/text()”
  • “条目/id/text()”
  • `doc.GetElementsByTagName(“条目”).SelectNodes(“id/text()”)I
  • 我可以通过使用childNodes进行迭代来访问id,但XPath会是什么呢


    但是如果我在整个文档上尝试“/*”,它会给我一个节点计数。为什么?

    xml中的xml元素在
    http://www.w3.org/2005/Atom
    xml名称空间。
    必须在
    XPATH
    语句中考虑此名称空间

    您必须使用
    XmlNamespaceManager
    注册此命名空间,并在
    XPATH
    语句中将所选前缀(此处:
    x
    )应用为:
    //x:feed/x:entry/x:id

    XmlDocument doc = new XmlDocument();
    String pathToYourXmlFile = @"c:\folder\file.xml";
    doc.Load(pathToYourXmlFile);
    
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
    nsmgr.AddNamespace("x", "http://www.w3.org/2005/Atom");
    XmlNodeList ids = doc.SelectNodes("//x:feed/x:entry/x:id", nsmgr);
    foreach (XmlNode id in ids)
    {
        Console.WriteLine(id.InnerText);
    }
    

    xml中的xml元素在
    http://www.w3.org/2005/Atom
    xml名称空间。
    必须在
    XPATH
    语句中考虑此名称空间

    您必须使用
    XmlNamespaceManager
    注册此命名空间,并在
    XPATH
    语句中将所选前缀(此处:
    x
    )应用为:
    //x:feed/x:entry/x:id

    XmlDocument doc = new XmlDocument();
    String pathToYourXmlFile = @"c:\folder\file.xml";
    doc.Load(pathToYourXmlFile);
    
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
    nsmgr.AddNamespace("x", "http://www.w3.org/2005/Atom");
    XmlNodeList ids = doc.SelectNodes("//x:feed/x:entry/x:id", nsmgr);
    foreach (XmlNode id in ids)
    {
        Console.WriteLine(id.InnerText);
    }
    

    您的xml包含名称空间
    xmlns=”http://www.w3.org/2005/Atom“
    在根级别节点

    您使用的是
    /feed/entry/id/text()
    这类XPath,但这些路径不适合此xml,因此无法获得任何所需的值

    您需要使用下面的XPath来获取所有
    节点的id

    var ids = doc.SelectNodes("//*[name()='feed']/*[name()='entry']/*[name()='id']/text()");
    
    在这里,我创建了一个示例控制台应用程序用于演示

    class program
    {
        public static void Main()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(@"Path to your xml file");
    
            var ids = doc.SelectNodes("//*[name()='feed']/*[name()='entry']/*[name()='id']/text()");
    
            foreach (XmlNode id in ids)
            {
                Console.WriteLine(id.Value);
            }
    
            Console.ReadLine();
        }
    }
    
    输出:


    您的xml包含名称空间
    xmlns=”http://www.w3.org/2005/Atom“
    在根级别节点

    您使用的是
    /feed/entry/id/text()
    这类XPath,但这些路径不适合此xml,因此无法获得任何所需的值

    您需要使用下面的XPath来获取所有
    节点的id

    var ids = doc.SelectNodes("//*[name()='feed']/*[name()='entry']/*[name()='id']/text()");
    
    在这里,我创建了一个示例控制台应用程序用于演示

    class program
    {
        public static void Main()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(@"Path to your xml file");
    
            var ids = doc.SelectNodes("//*[name()='feed']/*[name()='entry']/*[name()='id']/text()");
    
            foreach (XmlNode id in ids)
            {
                Console.WriteLine(id.Value);
            }
    
            Console.ReadLine();
        }
    }
    
    输出:


    您需要获取所有
    entry
    node的ID吗?@er shoaib是的,我添加了答案,以便仅使用单个XPath表达式直接获取所有ID文本。查看它可能对您有帮助您需要获取所有
    entry
    node的ID吗?@er shoaib是的,我添加了答案以直接使用单个XPath表达式获取所有ID文本。查看它可能对您有帮助谢谢澄清,让我处理您的更改谢谢澄清,让我处理您的更改