Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.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# 使用Linq从Xml获取特定节点_C#_Xml_Linq - Fatal编程技术网

C# 使用Linq从Xml获取特定节点

C# 使用Linq从Xml获取特定节点,c#,xml,linq,C#,Xml,Linq,我想使用Linq从XML响应中获取特定节点 我的Xml: 我的问题是systemXml.Elements(“Cube”)返回null。 这是我的web请求url您的根元素是DataSet,它有child,其中一个是Body,它有child多维数据集。您还需要指定元素的名称空间,即{}。这是为你工作的LINQ XDocument systemXml = XDocument.Load(path); XElement cube = (from cubeElement i

我想使用Linq从XML响应中获取特定节点 我的Xml:

我的问题是
systemXml.Elements(“Cube”)
返回
null

这是我的web请求url

您的根元素是DataSet,它有child,其中一个是Body,它有child多维数据集。您还需要指定元素的名称空间,即{}。这是为你工作的LINQ

       XDocument systemXml = XDocument.Load(path);

        XElement cube = (from cubeElement in systemXml.Elements("{http://www.bnr.ro/xsd}DataSet").Elements("{http://www.bnr.ro/xsd}Body").Elements("{http://www.bnr.ro/xsd}Cube")
                             where cubeElement.Attribute("date").Value.Equals("2017-01-03")
                         select cubeElement).Single();

        XElement rate = (from rateElement in cube.Elements("{http://www.bnr.ro/xsd}Rate")
                         where rateElement.Attribute("currency").Value.Equals("EUR")
                         select rateElement).Single();

享受它

看起来您需要名称空间

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication55
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://www.bnr.ro/files/xml/years/nbrfxrates2017.xml";

            XDocument systemXml = XDocument.Load(url);
            XNamespace ns = ((XElement)systemXml.FirstNode).GetDefaultNamespace();

            DateTime date = DateTime.Parse("2017-01-05");
            var results = systemXml.Descendants(ns + "Cube")
                .Where(x => ((DateTime)x.Attribute("date") == date))
                .Descendants(ns + "Rate")
                .Where(x => (string)x.Attribute("currency") == "EUR")
                .FirstOrDefault();
            var value = (decimal)results;

        }
    }

}

或者@NemanjaPerovic的可能重复,我明白了,但我对空值有一个问题:(,如果我写得不好,很抱歉..没问题。请尝试使用子体而不是元素。要了解原因,请查看文档或我添加的链接您的XML文件格式不正确您缺少很多结束标记。请更正它,以便我们能够提供帮助you@S.Petrosov,这是我的整个xmlfile@S.over17我已经更新了我的答案。现在它开始工作了ng,为什么不使用
systemXml.Root
而不是
((XElement)systemXml.FirstNode)
       XDocument systemXml = XDocument.Load(path);

        XElement cube = (from cubeElement in systemXml.Elements("{http://www.bnr.ro/xsd}DataSet").Elements("{http://www.bnr.ro/xsd}Body").Elements("{http://www.bnr.ro/xsd}Cube")
                             where cubeElement.Attribute("date").Value.Equals("2017-01-03")
                         select cubeElement).Single();

        XElement rate = (from rateElement in cube.Elements("{http://www.bnr.ro/xsd}Rate")
                         where rateElement.Attribute("currency").Value.Equals("EUR")
                         select rateElement).Single();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication55
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://www.bnr.ro/files/xml/years/nbrfxrates2017.xml";

            XDocument systemXml = XDocument.Load(url);
            XNamespace ns = ((XElement)systemXml.FirstNode).GetDefaultNamespace();

            DateTime date = DateTime.Parse("2017-01-05");
            var results = systemXml.Descendants(ns + "Cube")
                .Where(x => ((DateTime)x.Attribute("date") == date))
                .Descendants(ns + "Rate")
                .Where(x => (string)x.Attribute("currency") == "EUR")
                .FirstOrDefault();
            var value = (decimal)results;

        }
    }

}