<?xml-stylesheet href="latest_ob.xsl" type="text/xsl"?>
<current_observation version="1.0" >
</image>
<suggested_pickup>15 minutes after the hour</suggested_pickup>
<suggested_pickup_period>60</suggested_pickup_period>
<temp_f>44.0</temp_f>
<temp_c>6.7</temp_c>
<relative_humidity>55</relative_humidity>
<wind_string>North at 6.9 MPH (6 KT)</wind_string>
<wind_dir>North</wind_dir>
<wind_degrees>340</wind_degrees>
<wind_mph>6.9</wind_mph>
<wind_kt>6</wind_kt>
<pressure_string>1025.2 mb</pressure_string>
<pressure_mb>1025.2</pressure_mb>
<pressure_in>30.28</pressure_in>
<dewpoint_string>28.9 F (-1.7 C)</dewpoint_string>
<dewpoint_f>28.9</dewpoint_f>
<dewpoint_c>-1.7</dewpoint_c>
</current_observation>
我想从上面的xml数据中获取一些属性wind_dir
,wind_kt
等。我试过这个:
var dayt = GetXMLAsString(WeatherXML);
XDocument doc = XDocument.Parse(dayt);
var r = from element in doc.Elements()
where element.Name == "latitude"
select element;
foreach (var item in r)
{
Console.WriteLine(item.Value);
}
我希望将这些数据转换为数组或模型,以将其发送到ajax结果。
答案 0 :(得分:0)
我之前已尝试过,但保留了#34; .value&#34;:
var a =来自doc.Descendants中的哈希值(&#34;纬度&#34;) select hash.value;
当我尝试如下时,我得到了正确的结果: XmlDocument WeatherXML = new XmlDocument(); WeatherXML.Load(读取器);
var dayt = GetXMLAsString(WeatherXML);
XDocument doc = XDocument.Parse(dayt);
var a =来自doc.Descendants中的哈希值(&#34;纬度&#34;) 选择哈希;
var asdd = from hash in doc.Descendants("current_observation")
select hash;
答案 1 :(得分:0)
使用System.Xml.Linq命名空间,我们有一组方便的方法来调用。 在您的情况下,以下代码可以提供帮助:
var doc = XDocument.Parse(WeatherXML);
var value = from e in doc.Elements()
where e.Name == "wind_kt"
select e.Value;