Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

.net - C# extracting data from XML

I'm trying to read weather data from XML in a URL. The XML looks like this:

<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>

I am interested in the forecast data in the XML. I want to get the time from and time to, then the weather data. For example the temperature is written like this in the XML:

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

I want to extract the data with something like this:

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

I created sample code that is able to read everything but I don't know how to extract just the data I need. The code I have now looks like this:

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

            }
        }

Any ideas how I can extract just the weather data and the time?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use 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;
}

I haven't populated everything. I hope you get the idea of how to use it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...