简体   繁体   English

如何解析XML数据以检索子节点值?

[英]How to parse XML data to retrieve child nodes value?

I do have a xml file located at : http://api.wunderground.com/api/adaebe40743a9ca6/geolookup/conditions/forecast/q/India/Pilani.xml 我确实有一个xml文件,位于: http : //api.wunderground.com/api/adaebe40743a9ca6/geolookup/conditions/forecast/q/India/Pilani.xml

Now i want to fetch values at temp_c, relative_humidity, wind_string . 现在我想获取temp_c, relative_humidity, wind_string

For that i have created a class WeatherReader.cs as 为此,我创建了一个WeatherReader.cs类,作为

using System;
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml;

namespace CNGS
{
    public class WeatherReader
    {   public int Temp;
        public string Humidity;
        public string Wind;
        public string place;

        private void PopulateWeatherData()
        {
            XmlReader reader = XmlReader.Create("http://api.wunderground.com/api/adaebe40743a9ca6/geolookup/conditions/forecast/q/India/Pilani.xml");

            reader.MoveToContent();            

            while (reader.Read())
            {
                if (reader.LocalName == "temp_c")
                {
                    Temp = Convert.ToInt32(reader.Value);
                }
if (reader.LocalName == "relative_humidity")
                {
                    Humidity=reader.Value;
                }
if (reader.LocalName == "wind_string")
                {
                    Wind= reader.Value;
                }


            }

            reader.Close();
        }
    }
}

Is it correct, will it fetch the required values ? 是否正确,会获取所需的值吗?

Now since i want to show this information in a silverlight page. 现在,由于我想在Silverlight页面中显示此信息。 I tried to create an object of class weatherreader as 我试图创建一个类weatherreader的对象为

WeatherReader Weath = new WeatherReader();

but i don't know how to fetch the Temp, Wind values etc? 但我不知道如何获取“温度”,“风”值等? nothing like int tmp = Weath.Temp is working. 就像int tmp = Weath.Temp一样。

Please Help 请帮忙

I want to fetch the weather data and then use it in silverlight controls on MainPage, to show Live Weather Report. 我想获取天气数据,然后在MainPage的silverlight控件中使用它,以显示实时天气报告。

Thanks 谢谢

No that's not how you would use an XmlReader to read the document. 不,那不是使用XmlReader读取文档的方式。 It's a lot more complicated than that, in fact... too complicated. 事实上,这要复杂得多。

You're better off using LINQ to XML for this as it will be much simpler. 最好使用LINQ to XML,因为这样做会简单得多。

var xml = "http://api.wunderground.com/api/adaebe40743a9ca6/geolookup/conditions/forecast/q/India/Pilani.xml";
var doc = XDocument.Load(xml);
var currentObservation = doc.Element("response").Element("current_observation");
var temp = (int)currentObservation.Element("temp_c");
var humidity = (string)currentObservation.Element("relative_humidity");
var wind = (string)currentObservation.Element("wind_string");

If you wanted to use an XmlReader , then you'd have to do something like this: 如果要使用XmlReader ,则必须执行以下操作:

var xml = "http://api.wunderground.com/api/adaebe40743a9ca6/geolookup/conditions/forecast/q/India/Pilani.xml";
using (var reader = XmlReader.Create(xml))
{
    var temp = default(int);
    var humidity = default(string);
    var wind = default(string);

    string elementName = null;
    while (reader.Read())
    {
        switch (reader.NodeType)
        {
        case XmlNodeType.Element:
            elementName = reader.Name;
            break;
        case XmlNodeType.Text:
            switch (elementName)
            {
            case "temp_c":
                temp = reader.ReadContentAsInt();
                break;
            case "relative_humidity":
                humidity = reader.ReadContentAsString();
                break;
            case "wind_string":
                wind = reader.ReadContentAsString();
                break;
            }
            elementName = null;
            break;
        }
    }
}

This would be my version: 这将是我的版本:

XDocument doc = XDocument.Load("http://api.wunderground.com/api/adaebe40743a9ca6/geolookup/conditions/forecast/q/India/Pilani.xml");
var v = from d in doc.Element("response").Elements("current_observation") select new { Temp = d.Element("temp_c").Value, Humedity = d.Element("relative_humidity").Value, Wind = d.Element("wind_string").Value };
foreach (var c in v)
{
    Console.WriteLine(c.Temp);
}

Please try this it is not the most elegant solution but should work. 请尝试此操作,这不是最优雅的解决方案,但应该可以。

using System;
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Xml;

namespace CNGS
{
    public class WeatherReader
    {
        public int Temp;
        public string Humidity;
        public string Wind;
        public string place;

        public void PopulateWeatherData()
        {
            XmlReader reader = XmlReader.Create("http://api.wunderground.com/api/adaebe40743a9ca6/geolookup/conditions/forecast/q/India/Pilani.xml");

            bool IsNextTemp = false;
            bool IsHumidityTemp = false;
            bool IsWindTemp = false;

            reader.MoveToContent();

            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element: // The node is an element.
                        if (reader.Name == "temp_c")
                        {
                            IsHumidityTemp = false;
                            IsWindTemp = false;
                            IsNextTemp = true;
                        }
                        else if (reader.Name == "relative_humidity")
                        {
                            IsHumidityTemp = true;
                            IsWindTemp = false;
                            IsNextTemp = false;
                        }
                        else if (reader.Name == "wind_string")
                        {
                            IsHumidityTemp = false;
                            IsWindTemp = true;
                            IsNextTemp = false;
                        }
                        else
                        {
                            IsHumidityTemp = false;
                            IsWindTemp = false;
                            IsNextTemp = false;
                        }
                        break;
                    case XmlNodeType.Text: //Display the text in each element.
                        if (IsHumidityTemp)
                            this.Humidity = reader.Value;
                        else if (IsNextTemp)
                            this.Temp = int.Parse(reader.Value);
                        else if (IsWindTemp)
                            this.Wind = reader.Value;
                        break;
                }

            }

            reader.Close();
        }
    }
}

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

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