C#名称空间管理器不';无法使用命名空间解析XML

C#名称空间管理器不';无法使用命名空间解析XML,c#,xml,xpath,xml-namespaces,C#,Xml,Xpath,Xml Namespaces,我试图用C#解析下面采样的XML: 但是,节点始终为null。我已经尝试了很多选择,它为我提供的唯一选择是从原始XML中删除名称空间。但是我想直接解析源代码而不做任何修改 有三个问题需要纠正: 您错误定义了与gesmes关联的命名空间 改变 ns.AddNamespace("gesmes", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref"); 到 XPath没有考虑到Cube及其子体在默认名称空间中 为默认命名空间创建前缀: ns.A

我试图用C#解析下面采样的XML:


但是,
节点
始终为
null
。我已经尝试了很多选择,它为我提供的唯一选择是从原始XML中删除名称空间。但是我想直接解析源代码而不做任何修改

有三个问题需要纠正:

  • 您错误定义了与
    gesmes
    关联的命名空间

    改变

    ns.AddNamespace("gesmes", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");
    

  • XPath没有考虑到
    Cube
    及其子体在默认名称空间中

    为默认命名空间创建前缀:

    ns.AddNamespace("eu", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");
    
  • 使用来自#2的命名空间前缀更新XPath:


    Cube
    cubed?Linq XML总是让我不那么头疼:

    var doc = XDocument.Load("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
    string ns = "http://www.ecb.int/vocabulary/2002-08-01/eurofxref";
    
    var outerCube = doc.Root.Element(XName.Get("Cube", ns));
    var timeCube = outerCube.Element(XName.Get("Cube", ns));
    
    Console.WriteLine("Time: " + timeCube.Attribute("time").Value);
    
    foreach (var cube in timeCube.Elements())
    {
        Console.WriteLine(cube.Attribute("currency").Value + " => " + cube.Attribute("rate"));
    }
    

    我上个月刚为某人写了同样的代码。使用字典

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            const string URL = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";
            static void Main(string[] args)
            {
                XDocument doc = XDocument.Load(URL);
                XNamespace ns = doc.Root.GetDefaultNamespace();
    
                Dictionary<string, decimal> dict = doc.Descendants(ns + "Cube").Where(x => x.Attribute("currency") != null)
                    .GroupBy(x => (string)x.Attribute("currency"), y => (decimal)y.Attribute("rate"))
                    .ToDictionary(x => x.Key, y => y.FirstOrDefault());
            }
        }
    
    
    }
    
    使用系统;
    使用System.Collections.Generic;
    使用System.Linq;
    使用系统文本;
    使用System.Xml;
    使用System.Xml.Linq;
    命名空间控制台应用程序1
    {
    班级计划
    {
    常量字符串URL=”https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";
    静态void Main(字符串[]参数)
    {
    XDocument doc=XDocument.Load(URL);
    XNamespace ns=doc.Root.GetDefaultNamespace();
    Dictionary dict=doc.subjects(ns+“Cube”)。其中(x=>x.Attribute(“currency”)!=null)
    .GroupBy(x=>(字符串)x.Attribute(“货币”),y=>(十进制)y.Attribute(“汇率”)
    .ToDictionary(x=>x.Key,y=>y.FirstOrDefault());
    }
    }
    }
    
    ns.AddNamespace("eu", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");
    
    /gesmes:Envelope/eu:Cube/eu:Cube/eu:Cube
                     ^^^     ^^^     ^^^
    
    var doc = XDocument.Load("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
    string ns = "http://www.ecb.int/vocabulary/2002-08-01/eurofxref";
    
    var outerCube = doc.Root.Element(XName.Get("Cube", ns));
    var timeCube = outerCube.Element(XName.Get("Cube", ns));
    
    Console.WriteLine("Time: " + timeCube.Attribute("time").Value);
    
    foreach (var cube in timeCube.Elements())
    {
        Console.WriteLine(cube.Attribute("currency").Value + " => " + cube.Attribute("rate"));
    }
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            const string URL = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";
            static void Main(string[] args)
            {
                XDocument doc = XDocument.Load(URL);
                XNamespace ns = doc.Root.GetDefaultNamespace();
    
                Dictionary<string, decimal> dict = doc.Descendants(ns + "Cube").Where(x => x.Attribute("currency") != null)
                    .GroupBy(x => (string)x.Attribute("currency"), y => (decimal)y.Attribute("rate"))
                    .ToDictionary(x => x.Key, y => y.FirstOrDefault());
            }
        }
    
    
    }