C# 从 XML 中提取数据

标签 c# .net xml xml-parsing xmldocument

我正在尝试从 URL 中的 XML 中读取天气数据。 XML 如下所示:

<weatherdata>
<location>...</location>
<credit>...</credit>
<links>...</links>
<meta>...</meta>
<sun rise="2013-05-11T04:49:22" set="2013-05-11T21:39:03"/>
<forecast>
<text>...</text>
<tabular>
<time from="2013-05-11T01:00:00" to="2013-05-11T06:00:00" period="0">
<!--
 Valid from 2013-05-11T01:00:00 to 2013-05-11T06:00:00 
-->
<symbol number="2" name="Fair" var="mf/02n.03"/>
<precipitation value="0" minvalue="0" maxvalue="0.1"/>
<!--  Valid at 2013-05-11T01:00:00  -->
<windDirection deg="173.8" code="S" name="South"/>
<windSpeed mps="4.2" name="Gentle breeze"/>
<temperature unit="celsius" value="9"/>
<pressure unit="hPa" value="1004.2"/>
</time>
</tabular>
</forecast>
<observations>...</observations>
</weatherdata>

我对 XML 中的预测数据感兴趣。我想获取时间和时间,然后是天气数据。比如温度在XML中是这样写的:

<temperature unit="celsius" value="9"/>

我想用这样的方式提取数据:

 string fromTime = time from(the attribute in the xml);
                     string fromTime =time to(the attribute in the xml);
                    string name = temperature(the attribute in the xml);
                    string unit =unit(the attribute in the xml);
                    int value = value(the attribute in the xml);

我创建了能够读取所有内容的示例代码,但我不知道如何只提取我需要的数据。我现在的代码如下所示:

        String URLString = "http://www.yr.no/place/Norway/Oslo/Oslo/Oslo/forecast.xml";
        XmlTextReader reader = new XmlTextReader(URLString);

        while (reader.Read())
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Element: // The node is an element.
                    Console.Write("" + reader.Name);

                    while (reader.MoveToNextAttribute()) // Read the attributes.
                        Console.Write(" " + reader.Name + "='" + reader.Value + "'");
                    Console.Write("\n");
                    Console.WriteLine("------------------------------");
                    break;
                case XmlNodeType.Text: //Display the text in each element.
                    Console.WriteLine(reader.Value);
                    break;
                case XmlNodeType.EndElement: //Display the end of the element.
                    Console.Write("</" + reader.Name);
                    Console.WriteLine(">");
                    break;

            }
        }

有什么办法可以只提取天气数据和时间吗?

最佳答案

使用 LINQ to XML

XDocument X = XDocument.Load("http://www.yr.no/place/Norway/Oslo/Oslo/Oslo/forecast.xml");

var forecast = X.Element("weatherdata").Element("forecast");
var location = forecast.Descendants("location").Attributes("name").FirstOrDefault().Value;
var tempData = forecast.Element("tabular").Elements("time");

//This is what you need
var data = tempData.Select(item=>
            new{
                from = Convert.ToDateTime(item.Attribute("from").Value),
                to = Convert.ToDateTime(item.Attribute("to").Value),
                temp = item.Element("temperature").Attribute("value").Value
            });


//Or you can do a foreach if you need to
foreach (var item in tempData)
{
        DateTime from = Convert.ToDateTime(item.Attribute("from").Value);
        var temp = item.Element("temperature").Attribute("value").Value;
}

我还没有填充所有内容。我希望您了解如何使用它。

关于C# 从 XML 中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16491811/

相关文章:

.net - 装饰器不适用于 RegisterAll 中指定的实现类型

java - 我无法将我的 id 从我的 main.xml 文件带到我的 main.java 文件

c# - 卸载 C# Windows 服务 - 使用卸载程序

C# 如何在用户控件上显示文本气泡?

c# - 如何使用 TrueForAll

c# - CAPICOM TripleDES 和 System.Security.Cryptography TripleDES 之间的区别

java - 使用 Transformer 处理空 CDATA 时出现 IndexOutOfBoundsException

java - Woodstox StAX - 如何关闭文本内容验证?

c# - 在 XAML 中设置 <Window.DataContext>

c# - DDD 投屏问题?