C# 在Asp.net中读取XML不会得到任何结果

C# 在Asp.net中读取XML不会得到任何结果,c#,asp.net,C#,Asp.net,我使用以下代码从中读取XML文件 问题是我什么都不懂nodeList.Count总是给我0 您需要正确处理名称空间。 处理它们的方法可能不止一种,这是一种 XmlDocument xdoc = new XmlDocument(); xdoc.Load("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"); XmlNamespaceManager xnm = new XmlNamespace

我使用以下代码从中读取XML文件


问题是我什么都不懂
nodeList.Count
总是给我
0

您需要正确处理名称空间。 处理它们的方法可能不止一种,这是一种

XmlDocument xdoc = new XmlDocument();
        xdoc.Load("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
        XmlNamespaceManager xnm = new XmlNamespaceManager(xdoc.NameTable);
        xnm.AddNamespace("gesmes", "http://www.gesmes.org/xml/2002-08-01");
        xnm.AddNamespace("eurofxref", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");

        XmlNodeList nodeList = xdoc.DocumentElement.SelectNodes("//gesmes:Envelope/eurofxref:Cube/eurofxref:Cube", xnm);

        if (nodeList == null)
        { 
            var text = "node is null";
        }
        foreach (XmlNode node in nodeList)
        {
            XmlNode innerNode = node.SelectSingleNode(".//eurofxref:Cube", xnm);

            var text = innerNode.Attributes["currency"].Value;
        }

我不知道为什么会这么复杂,但是

XmlDocument xdoc = new XmlDocument();

xdoc.Load("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");

XmlNamespaceManager xMan = new XmlNamespaceManager(xdoc.NameTable);

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

XmlNodeList nodeList = xdoc.DocumentElement.SelectNodes("//def:Cube", xMan);

这些答案对你有帮助吗?@fhogberg:有。你的那个:)
XmlDocument xdoc = new XmlDocument();

xdoc.Load("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");

XmlNamespaceManager xMan = new XmlNamespaceManager(xdoc.NameTable);

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

XmlNodeList nodeList = xdoc.DocumentElement.SelectNodes("//def:Cube", xMan);