如何在C#中使用XML前缀

本文关键字:XML 前缀 | 更新日期: 2023-09-27 18:15:23

编辑:我现在已经发布了我的应用程序:http://pastebin.com/PYAxaTHU

我正试图制作一个基于控制台的应用程序来返回我的温度。

using System;
using System.Xml;

namespace GetTemp
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(downloadWebPage(
             "http://www.andrewmock.com/uploads/example.xml"
            ));
            XmlNamespaceManager man = new XmlNamespaceManager(doc.NameTable);
            man.AddNamespace("aws", "www.aws.com/aws");
            XmlNode weather = doc.SelectSingleNode("aws:weather", man);
            Console.WriteLine(weather.InnerText);
            Console.ReadKey(false);
        }

    }
}

以下是示例XML:

<aws:weather xmlns:aws="http://www.aws.com/aws">
   <aws:api version="2.0"/>
   <aws:WebURL>http://weather.weatherbug.com/WA/Kenmore-weather.html?ZCode=Z5546&Units=0&stat=BOTHL</aws:WebURL>
   <aws:InputLocationURL>http://weather.weatherbug.com/WA/Kenmore-weather.html?ZCode=Z5546&Units=0</aws:InputLocationURL>
   <aws:station requestedID="BOTHL" id="BOTHL" name="Moorlands ES" city="Kenmore" state=" WA" zipcode="98028" country="USA" latitude="47.7383346557617" longitude="-122.230278015137"/>
   <aws:current-condition icon="http://deskwx.weatherbug.com/images/Forecast/icons/cond024.gif">Mostly Cloudy</aws:current-condition>
   <aws:temp units="&deg;F">40.2</aws:temp>
   <aws:rain-today units=""">0</aws:rain-today>
   <aws:wind-speed units="mph">0</aws:wind-speed>
   <aws:wind-direction>WNW</aws:wind-direction>
   <aws:gust-speed units="mph">5</aws:gust-speed>
   <aws:gust-direction>NW</aws:gust-direction>
</aws:weather>

我只是不确定如何在这里正确使用XML前缀。这是怎么回事?

如何在C#中使用XML前缀

OK,因此基于示例中的XML:

<aws:weather xmlns:aws="http://www.aws.com/aws">
   <aws:api version="2.0"/>
   <aws:WebURL>http://weather.weatherbug.com/WA/Kenmore-weather.html?ZCode=Z5546&Units=0&stat=BOTHL</aws:WebURL>
   <aws:InputLocationURL>http://weather.weatherbug.com/WA/Kenmore-weather.html?ZCode=Z5546&Units=0</aws:InputLocationURL>
   <aws:station requestedID="BOTHL" id="BOTHL" name="Moorlands ES" city="Kenmore" state=" WA" zipcode="98028" country="USA" latitude="47.7383346557617" longitude="-122.230278015137"/>
   <aws:current-condition icon="http://deskwx.weatherbug.com/images/Forecast/icons/cond024.gif">Mostly Cloudy</aws:current-condition>
   <aws:temp units="&deg;F">40.2</aws:temp>
   <aws:rain-today units=""">0</aws:rain-today>
   <aws:wind-speed units="mph">0</aws:wind-speed>
   <aws:wind-direction>WNW</aws:wind-direction>
   <aws:gust-speed units="mph">5</aws:gust-speed>
   <aws:gust-direction>NW</aws:gust-direction>
</aws:weather>

你想读出哪个值?

您的代码的问题在于您的XML名称空间是错误的:

您有:

XmlNamespaceManager man = new XmlNamespaceManager(doc.NameTable);
man.AddNamespace("aws", "www.aws.com/aws");

但是XML名称空间是:http://www.aws.com/aws

所以你应该有:

 XmlNamespaceManager man = new XmlNamespaceManager(doc.NameTable);
 man.AddNamespace("aws", "http://www.aws.com/aws");

因此,要读出例如温度,请使用以下内容:

XmlDocument doc = new XmlDocument();
doc.LoadXml(downloadWebPage("http://www.andrewmock.com/uploads/9/1/0/7/9107466/example.xml"));
XmlNamespaceManager man = new XmlNamespaceManager(doc.NameTable);
man.AddNamespace("aws", "http://www.aws.com/aws");
XmlNode temps = doc.SelectSingleNode("/aws:weather/aws:temp", man);
string tempValue = temps.InnerText;

tempValue 中为您提供"40.2"的值

正如Henk Holtermann在他的评论中所建议的那样,使用Linq to XML:阅读这篇文章会更容易

XDocument doc = XDocument.Load("http://www.andrewmock.com/uploads/9/1/0/7/9107466/example.xml");
XNamespace aws = "http://www.aws.com/aws";
var weatherNode = doc.Document.Descendants(aws + "weather");
var tempNode = weatherNode.Descendants(aws + "temp").FirstOrDefault();
string tempValue = tempNode.Value;

当然,这还不包括任何错误处理(检查weatherNode是否为NULL之类的东西(,但它给了您一个想法。