简体   繁体   English

C#如何获取所有XML值

[英]C# how to get all XML Value

I've got an XML file我有一个 XML 文件

<forecast>
<time from="2020-04-05T03:00:00" >
<temperature unit="celsius" value="9.46" min="8.84" max="9.46"/>
<clouds value="overcast clouds" all="96" unit="%"/>
</time>
<time from="2020-04-05T09:00:00">
<temperature unit="celsius" value="9.53" min="9.22" max="9.53"/>
<clouds value="scattered clouds" all="49" unit="%"/>
</time>
</forecast>

working with c#使用 c#

foreach (var npc in doc.Descendants("forecast"))
{
    dt.Rows.Add(new object[]
    {
        (DateTime) npc.Descendants("time").First().Attribute("from"),
        (string) npc.Descendants("temperature").First().Attribute("value"),
        (string) npc.Descendants("clouds").First().Attribute("value")
    });
}

I got first value only,what is the method for looping all value?我只有第一个值,循环所有值的方法是什么?

Try using below code to get all node list:尝试使用以下代码获取所有节点列表:

    XmlNodeList xnList = xmlDoc.SelectNodes("/forecast/time");

Then loop through it.然后循环遍历它。

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

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