从C#LINQ到XML的国家气象服务SOAP服务解析XML

时间:2009-02-04 21:25:44

标签: xml linq c#-3.0 weather

我是一名初级C#程序员,正在尝试开发一个库,允许我封装解析从NWS返回的XML的令人讨厌的细节,并返回表示数据的集合。

我的SOAP请求将以这种形式返回XML文档:

<?xml version="1.0" encoding="UTF-8"?>
<dwml version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.nws.noaa.gov/forecasts/xml/DWMLgen/schema/DWML.xsd">
  <head>
    <product srsName="WGS 1984" concise-name="time-series" operational-mode="official">
      <title>NOAA's National Weather Service Forecast Data</title>
      <field>meteorological</field>
      <category>forecast</category>
      <creation-date refresh-frequency="PT1H">2009-02-04T20:01:00Z</creation-date>
    </product>
    <source>
      <more-information>http://www.nws.noaa.gov/forecasts/xml/</more-information>
      <production-center>Meteorological Development Laboratory<sub-center>Product Generation Branch</sub-center></production-center>
      <disclaimer>http://www.nws.noaa.gov/disclaimer.html</disclaimer>
      <credit>http://www.weather.gov/</credit>
      <credit-logo>http://www.weather.gov/images/xml_logo.gif</credit-logo>
      <feedback>http://www.weather.gov/feedback.php</feedback>
    </source>
  </head>
  <data>
    <location>
      <location-key>point1</location-key>
      <point latitude="42.23" longitude="-83.27"/>
    </location>
    <moreWeatherInformation applicable-location="point1">http://forecast.weather.gov/MapClick.php?textField1=42.23&amp;textField2=-83.27</moreWeatherInformation>
    <time-layout time-coordinate="local" summarization="none">
      <layout-key>k-p24h-n7-1</layout-key>
      <start-valid-time>2009-02-04T07:00:00-05:00</start-valid-time>
      <end-valid-time>2009-02-04T19:00:00-05:00</end-valid-time>
      <start-valid-time>2009-02-05T07:00:00-05:00</start-valid-time>
      <end-valid-time>2009-02-05T19:00:00-05:00</end-valid-time>
      <start-valid-time>2009-02-06T07:00:00-05:00</start-valid-time>
      <end-valid-time>2009-02-06T19:00:00-05:00</end-valid-time>
      <start-valid-time>2009-02-07T07:00:00-05:00</start-valid-time>
      <end-valid-time>2009-02-07T19:00:00-05:00</end-valid-time>
      <start-valid-time>2009-02-08T07:00:00-05:00</start-valid-time>
      <end-valid-time>2009-02-08T19:00:00-05:00</end-valid-time>
      <start-valid-time>2009-02-09T07:00:00-05:00</start-valid-time>
      <end-valid-time>2009-02-09T19:00:00-05:00</end-valid-time>
      <start-valid-time>2009-02-10T07:00:00-05:00</start-valid-time>
      <end-valid-time>2009-02-10T19:00:00-05:00</end-valid-time>
    </time-layout>
    <time-layout time-coordinate="local" summarization="none">
      <layout-key>k-p24h-n6-2</layout-key>
      <start-valid-time>2009-02-04T19:00:00-05:00</start-valid-time>
      <end-valid-time>2009-02-05T08:00:00-05:00</end-valid-time>
      <start-valid-time>2009-02-05T19:00:00-05:00</start-valid-time>
      <end-valid-time>2009-02-06T08:00:00-05:00</end-valid-time>
      <start-valid-time>2009-02-06T19:00:00-05:00</start-valid-time>
      <end-valid-time>2009-02-07T08:00:00-05:00</end-valid-time>
      <start-valid-time>2009-02-07T19:00:00-05:00</start-valid-time>
      <end-valid-time>2009-02-08T08:00:00-05:00</end-valid-time>
      <start-valid-time>2009-02-08T19:00:00-05:00</start-valid-time>
      <end-valid-time>2009-02-09T08:00:00-05:00</end-valid-time>
      <start-valid-time>2009-02-09T19:00:00-05:00</start-valid-time>
      <end-valid-time>2009-02-10T08:00:00-05:00</end-valid-time>
    </time-layout>
    <parameters applicable-location="point1">
      <temperature type="maximum" units="Fahrenheit" time-layout="k-p24h-n7-1">
        <name>Daily Maximum Temperature</name>
        <value>15</value>
        <value>19</value>
        <value>33</value>
        <value>46</value>
        <value>41</value>
        <value>43</value>
        <value>44</value>
      </temperature>
      <temperature type="minimum" units="Fahrenheit" time-layout="k-p24h-n6-2">
        <name>Daily Minimum Temperature</name>
        <value>-2</value>
        <value>16</value>
        <value>29</value>
        <value>32</value>
        <value>27</value>
        <value>32</value>
      </temperature>
    </parameters>
  </data>
</dwml>

我正在尝试将此XML中的最大和最小临时值放在单独的字符串集合中,而忽略使用LINQ的“name”元素。

编辑: 这是我用来从Web Reference获取XML的代码:

    WeatherNWS.ndfdXML client = new TestNWS.WeatherNWS.ndfdXML();

    string XMLZip = client.LatLonListZipCode("48180");

    XElement myElement = XElement.Parse(XMLZip);
    string[] myString = myElement.Value.Split(',');

    decimal lat = Convert.ToDecimal(myString[0]);
    decimal lon = Convert.ToDecimal(myString[1]);

    weatherParametersType parameters = new weatherParametersType();

    parameters.maxt = true;
    parameters.mint = true;

    string XML = client.NDFDgen(lat, lon, productType.timeseries, DateTime.Now, DateTime.Now.AddDays(7), parameters);

2 个答案:

答案 0 :(得分:3)

这应该能够为您获得最高温度,您只需要更改where过滤器以获得最低温度

 using System.Xml.Linq;
 \\...

 XDocument xmlDoc = XDocument.Load("YourXml.xml");
 var maximums = from tempvalue in xmlDoc.Descendants("temperature").Elements("value")
                           where tempvalue.Parent.Attribute("type").Value == "maximum"
                           select (string)tempvalue;

List<string> returnme = maximums.ToList<string>();
return returnme;

希望得到这个帮助。

注意:我在C#中使用Linq to Xml有点生疏,所以它可能不是最优雅的解决方案。

答案 1 :(得分:0)

您可能需要解析使用xpath返回的xml来提取所需的数据。有一个简短的例子here

或者你可以创建一个镜像xml结构的类结构,并使用XmlSerializer将xml反序列化为对象,但在这种情况下,这可能比它的价值更麻烦。