使用XmlDocument从XML中提取数据

时间:2015-04-15 15:21:55

标签: c# .net xml

我在c#中使用天气网络服务。我正在通过它Lat-Long并且它返回预测的最大值&该地区的最低温度。以下是我正在使用的代码

 var response = client.ndfdGen(latlong);
 XmlDocument doc = new XmlDocument();
 doc.LoadXml(response);

以下是响应数据,我得到了xml response 在此响应中,有纬度和经度。 我必须提取

<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
   <SOAP-ENV:Body>
      <ns1:NDFDgenResponse xmlns:ns1="http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl">
         <dwmlOut xsi:type="xsd:string"><![CDATA[<?xml version="1.0"?>
<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">2015-04-15T15:13:07Z</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="39.01" longitude="-77.02"/>
    </location>
    <moreWeatherInformation applicable-location="point1">http://forecast.weather.gov/MapClick.php?textField1=39.01&amp;textField2=-77.02</moreWeatherInformation>
    <time-layout time-coordinate="local" summarization="none">
      <layout-key>k-p24h-n2-1</layout-key>
      <start-valid-time>2015-04-17T08:00:00-04:00</start-valid-time>
      <end-valid-time>2015-04-17T20:00:00-04:00</end-valid-time>
      <start-valid-time>2015-04-18T08:00:00-04:00</start-valid-time>
      <end-valid-time>2015-04-18T20:00:00-04:00</end-valid-time>
    </time-layout>
    <parameters applicable-location="point1">
      <temperature type="maximum" units="Fahrenheit" time-layout="k-p24h-n2-1">
        <name>Daily Maximum Temperature</name>
        <value>68</value>
        <value>71</value>
      </temperature>
    </parameters>
  </data>
</dwml>]]></dwmlOut>
      </ns1:NDFDgenResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我想从<time-layout time-coordinate="local" summarization="none">中提取start-valid-time中的信息,例如end-valid-timetemperature<temperature type="maximum" units="Fahrenheit" time-layout="k-p24h-n2-1"> {{1}}。{/ p>

如何联系这些节点并对其进行迭代?

2 个答案:

答案 0 :(得分:1)

正如评论中所建议的那样,使用XDocument可以访问为此目的而构建的许多LINQ-to-XML方法:

// load up the xml into an XDocument
var response = client.ndfdGen(latlong);
var originalDocument = XDocument.Parse(response);

// extract cdata
var cdata = originalDocument.DescendantNodes().OfType<XCData>().First().Value;
var cdataDocument = XDocument.Parse(cdata);

// find the right element via xpath
var myElement = cdataDocument.Root.XPathSelectElement("//dwml/data/location/point");
return myElement.Attribute("latitude").Value;

请注意,在xPath中使用“//”运算符没有很好的性能。一旦获得概念验证工作,请尝试确定绝对路径。可以在MSDN

上找到有关xPath操作的说明

答案 1 :(得分:1)

您将首先必须提取CDATA,这是唯一的特殊挑战 - 然后您可以使用XmlDocument或XDocument或XmlReader。我建议这样做:

var response = client.ndfdGen(latlong);
XDocument xd = null;

using (XmlReader xr = XmlReader.Create(new StringReader(response))) // load the response into an XmlReader
{
    xr.ReadToDescendant("dwmlOut"); // go to the dwmlOut node
    xr.Read(); // move to the CDATA in that node
    xd = XDocument.Parse(xr.Value); // load **that** XML into your XDocument
}

string startTime = xd.Descendants("start-valid-time").First().Value;

等等。

如果你坚持使用XmlDocument,你可以在这里使用相同的方法,只做XmlDocument.LoadFrom(xr.Value),但XDocument API更灵活一些,并且可以更好地执行。