Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XML
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;     
 
                }
            }
        }
       
    }
 
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 22-Sep-12 7:50am
Comments
Ed Nutting at 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
Tadit Dash at 22-Sep-12 16:08pm
   
Exactly...
So @Minghang, please check what values you are getting while debugging and let us know.
 
Thanks...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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>
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 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);   
            }
        }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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

  Permalink  

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

  Print Answers RSS
0 OriginalGriff 278
1 Sergey Alexandrovich Kryukov 236
2 PhilLenoir 179
3 ChauhanAjay 166
4 ClimerChinna 150
0 Sergey Alexandrovich Kryukov 6,140
1 OriginalGriff 5,195
2 CPallini 2,473
3 Richard MacCutchan 1,607
4 Abhinav S 1,505


Advertise | Privacy | Mobile
Web02 | 2.8.140814.1 | Last Updated 23 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100