如何在C#中使用XML前缀?

时间:2011-11-19 21:00:20

标签: c# .net xml xmldocument

  

编辑:我现在发布了我的应用: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前缀。这有什么问题?

1 个答案:

答案 0 :(得分:12)

好的,所以基于示例中的这个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;

"40.2"

中为您提供tempValue的值

正如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等事情) - 但它会给你一个想法。