C# 使用Linq从Xml获取特定节点
我想使用Linq从XML响应中获取特定节点 我的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
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;
}
}
}