Click here to Skip to main content
15,888,124 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
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.

C#
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;     

                }
            }
        }
       
    }


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
Posted
Comments
Ed Nutting 22-Sep-12 15:13pm    
Have you debugged and checked what the value of node.Name is? We can't help much without knowing that that value is what it is supposed to be. Have you also checked that you don't have a capitalisation issue. I would recommend:

switch(node.Name.ToLower()) and then put all your options in lower case e.g. "city" instead of "City".

Ed
Exactly...
So @Minghang, please check what values you are getting while debugging and let us know.

Thanks...

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.

C#
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);
    }
}
 
Share this answer
 
Thankyou all for you comments...it really helped and encouraged me to look further. i solved my issue using the following code.

C#
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;
                    
            }

        }
       
    }
 
Share this answer
 
The structure of the xml file returned by the web service is as follows:

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

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900