0

次のコードがあります。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>
4

2 に答える 2

1

プロパティを呼び出す必要がありValueます。

さらに、timeノード内のアトリビュートの名前は"from""to"です。という名前のものは見当たりません"time"

Addステートメントを次のように変更します。

list.Add(element.Attribute("from").Value);
于 2013-06-20T18:42:27.640 に答える
-1

XPath はあなたの友達です。

XmlNode foo = xdoc.SelectSingleNode("/weatherdata/forecast/time/clouds");
于 2013-06-20T18:46:06.750 に答える