簡體   English   中英

無法從C#中的xml文件獲取數據(Windows Phone 8)

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

我有以下代碼。 如何從XML文件中獲取雲的價值? 剛嘗試吃東西。 列表中填充了正確數量的元素,但它們都是空的。

 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"));

 }

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>

您必須調用Value屬性。

另外, time節點中屬性的名稱是"from""to" 我看不到任何名稱為"time"

將您的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