简体   繁体   English

无法从C#中的xml文件获取数据(Windows Phone 8)

[英]Can't get data from xml file in C# (Windows phone 8)

I Have the following code. 我有以下代码。 How can i get the value of clouds from the XML file? 如何从XML文件中获取云的价值? Just tried someting. 刚尝试吃东西。 The list is filled with the right number of elements, but they are al empty. 列表中填充了正确数量的元素,但它们都是空的。

 XDocument xdoc = XDocument.Parse(e.Result, LoadOptions.None);

 List<string> list = new List<string>();

 foreach (XElement element in xdoc.Root.Elements("forecast").Elements("time"))
 {
     list.Add((string)element.Attribute("time"));

 }

The xml file: xml文件:

<weatherdata>
    <location>
        <name>Dronten</name>
        <type/>
        <country>NL</country>
        <timezone/>
        <location altitude="0" latitude="52.525002" longitude="5.71806" geobase="geonames" geobaseid="0"/>
    </location>
    <credit/>
    <meta>
        <lastupdate>2013-06-20T17:30:39</lastupdate>
        <calctime>0.0547</calctime>
        <nextupdate>2013-06-20T20:30:39</nextupdate>
    </meta>
    <sun rise="2013-06-20T03:13:40" set="2013-06-20T20:04:01"/>
    <forecast>
        <time from="2013-06-20T15:00:00" to="2013-06-20T18:00:00">
            <symbol number="801" name="few clouds" var="02d"/>
            <precipitation/>
            <windDirection deg="43.5014" code="NE" name="NorthEast"/>
            <windSpeed mps="6.9" name="Moderate breeze"/>
            <temperature unit="celsius" value="19.71" min="19.71" max="26.517"/>
            <pressure unit="hPa" value="1022.12"/>
            <humidity value="62" unit="%"/>
            <clouds value="few clouds" all="24" unit="%"/>
        </time>
        <time from="2013-06-20T18:00:00" to="2013-06-20T21:00:00">
            <symbol number="501" name="moderate rain" var="10d"/>
            <precipitation value="10.5" unit="3h" type="rain"/>
            <windDirection deg="18.0018" code="NNE" name="North-northeast"/>
            <windSpeed mps="3.72" name="Gentle Breeze"/>
            <temperature unit="celsius" value="13.24" min="13.24" max="19.706"/>
            <pressure unit="hPa" value="1021.71"/>
            <humidity value="100" unit="%"/>
            <clouds value="overcast clouds" all="92" unit="%"/>
        </time>
    </forecast>
</weatherdata>

you have to invoke the Value property. 您必须调用Value属性。

Additionally, the names of the attributes in your time nodes are "from" and "to" ; 另外, time节点中属性的名称是"from""to" I don't see any of them with the name "time" . 我看不到任何名称为"time"

change your Add statement to: 将您的Add语句更改为:

list.Add(element.Attribute("from").Value);

XPath是您的朋友。

XmlNode foo = xdoc.SelectSingleNode("/weatherdata/forecast/time/clouds");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM