从xmlElement读取 [英] Reading from an xmlElement

查看:79
本文介绍了从xmlElement读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我想读取从Web服务返回的xmlelement形式的节点(返回类型为system.xml.xmlelement),并将其分配给标签.我已经编写了以下代码.

Hello all, i want to read nodes form an xmlelement that is returned from a webservice(return type is system.xml.xmlelement) and assign it to labels. I have written the following code.

private localhost.WeatherService Weather = new localhost.WeatherService();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            LoadXmlData("SomeCity");
    }
 public void LoadXmlData(string strCity)
    {
        XmlElement xData = Weather.GetWeatherCondition(strCity);
        

        if (xData != null)
        {
            foreach (XmlNode node in xData.ChildNodes)
            {
                switch (node.Name)
                {
                    case "City":
                        lblCity.Text = node.InnerText;
                        break;
                    case "Day":
                        lblDay.Text = node.InnerText;
                        break;
                    case "Condition":
                        lblCondition.Text = node.InnerText;
                        break;     

                }
            }
        }
       
    }



我的问题是标签未更新...即无法识别节点名称.我在这里做错了什么?我已经检查过,并且Web服务很好地返回了xml数据.
在此先感谢.
Mingsho Limbu



the problem i have is the labels are not updated...i.e the nodename is not recognized. what am i doing wrong here? i have checked and the web service is returning the xml data fine.
Thanks in Advance.
Mingsho Limbu

推荐答案

我认为未设置标签的原因是ChildNodes方法仅提供直接子节点(低1级)而不提供子节点的孩子们.您可以使用代码的递归实现来解决它.

I think the reason that the labels aren''t set, is that the ChildNodes method only gives the direct childnodes (1 level lower) and not children of the children. You could solve it with a recursive implemenation of you code.

public void LoadXmlData(string strCity)
{
    XmlElement xData = Weather.GetWeatherCondition(strCity);

    AnalyseData(xData as XmlNode);
}

private void AnalyseData(XmlNode node)
{
    foreach (XmlNode subnode in node.ChildNodes)
    {

        switch (subnode.Name)
        {
            case "m:City":
                lblCity.Text = subnode.InnerText;
                break;
            case "m:Day":
                lblDay.Text = subnode.InnerText;
                break;
            case "m:Condition":
                lblCondition.Text = subnode.InnerText;
                break;
        }

        if (subnode.HasChildNodes)
            AnalyseData(subnode);
    }
}


谢谢大家的评论……它确实有所帮助,并鼓励我进一步研究.我使用以下代码解决了我的问题.

Thankyou all for you comments...it really helped and encouraged me to look further. i solved my issue using the following code.

private localhost.WeatherService Weather = new localhost.WeatherService();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            LoadXmlData("SomeCity");
    }

    public void LoadXmlData(string strCity)
    {
        XmlElement xData = Weather.GetWeatherCondition(strCity);

        for(int nIndex=0;nIndex<xdata.childnodes.count;nindex++)>
        {
            XmlNode xNode = xData.ChildNodes[nIndex];

            if (xNode.Name.ToLower() == "conditions")
            {
                XmlNodeList xList = xNode.SelectNodes("City");
                foreach (XmlNode nodeChild in xList)
                {
                    //if (nodeChild.Name.ToLower() == "city")
                        lblCity.Text = nodeChild.InnerText;
                }
                xList = xNode.SelectNodes("Day");
                foreach (XmlNode nodeChild in xList)
                    lblDay.Text = nodeChild.InnerText;
                xList = xNode.SelectNodes("Condition");
                foreach (XmlNode nodeChild in xList)
                    lblCondition.Text = nodeChild.InnerText;
                xList = xNode.SelectNodes("Fahrenheit");
                foreach (XmlNode nodeChild in xList)
                    lblF.Text = nodeChild.InnerText;
                xList = xNode.SelectNodes("Centigrade");
                foreach (XmlNode nodeChild in xList)
                    lblC.Text = nodeChild.InnerText;
                xList = xNode.SelectNodes("Humidity");
                foreach (XmlNode nodeChild in xList)
                    lblHumidity.Text = nodeChild.InnerText;
                xList = xNode.SelectNodes("Wind");
                foreach (XmlNode nodeChild in xList)
                    lblWind.Text = nodeChild.InnerText;
                    
            }

        }
       
    }


Web服务返回的xml文件的结构如下:

The structure of the xml file returned by the web service is as follows:

<weather_condition>
  <condtions>
    <city>New York</city>
    <day>Saturday</day>
    <condtion>Patly Cloudy</condtion>
    <fahrenheit>75</fahrenheit>
    <centigrade>32</centigrade>
    <humidity>71%</humidity>
    <wind>wind from sounth at 2mph</wind>
 </condtions>
</weather_condition>


我已经尝试过按螺母化的建议将其大写,但这没有用.
调试文件并按照 Martin 的建议进行操作后,我发现输入条件标签后,它刚好从开关盒中出来,并将所有数据放入湿度标签中,而所有其他标签为空白.


I''ve tried capitalizing as suggested by ed nutting but that didn''t work.
after debugging the file and doing as suggested by Martin i found out that after entering the conditions tag it just comes out of the switch block and places all the data in the label for humidity, while all the other labels are blank.


这篇关于从xmlElement读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆