Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello CodeProject.
I have a slight problem with XML. Tried to create an application which could show temperatures in different cities.

This is my XML table:
XML
<SERVERS>
 <SERVER name="1">
  <url>http://someeather.com/database.xml</url>
  <search>xml/searchpath/here</search>
 </SERVER>
 <SERVER name="2">
  <url>http://otherweather.com/database.xml</url>
  <search>xml/searchpath/here</search>
 </SERVER>
 <SERVER name="3">
  <url>http://anotherweather.com/database.xml</url>
  <search>xml/searchpath/here</search>
 </SERVER>
</SERVERS>


What I must do, is:
1. Get the temperature from each server, based on the search node
2. write it out.

I know how to get the 'responsedoc' and get the data from the server, I just don't know how to get the responsedoc for each one of these servers separately.

My code looks like this:
C#
XmlNodeList urls = serverDoc.SelectNodes("//servers/server");
string id1 = "1;
string id2 = "2";
string id3 = "3";
foreach(XmlNode node in urls)
{
    string server1 = node.Attributes["name"].InnerText; //Get the attribute name
    //Check if attribute value matches the 'id' provided before foreach.
    if (server1 == name1)
    {
        //Create new node list, so we can get the server url.
        XmlNodeList urls2 = serverDoc.SelectNodes("//servers/server");
        foreach (XmlNode node2 in urls2)
        {
            var serverPath = temp.InnerText;                                //Get the serverpath (the <search> node in .xml)
            WebRequest request = WebRequest.Create(node2["url"].InnerText); //Get the nodes with the name <url>.
            var response = request.GetResponse();                           //Get a response from the server, based on the request node url, that matched the 'name' attribute with the 'id' before this code.
            Stream stream = response.GetResponseStream();                   //Create new stream from the external server for reading
            XmlDocument responseDoc = new XmlDocument();                    //Create new instance for a new xml document
            responseDoc.Load(stream);                                       //Create new xml document from the stream
            var tempOut = responseDoc.SelectSingleNode(serverPath);         //From this new document get the temperature based on the searchpath provided befre this code.
            string temperature = tempOut.Attributes["value"].Value;         //Now get the value from the attribute with the name 'value' from that new xml document, to set the temperature.
            SetTemperature1(temperature);   //Set the value on our method which sets this value on the templbl on our mainview.
        }
    }
}

Because of this, I'm getting the same temperature for all the other servers, but I need different values for each (obvious). Might be something wrong with my code, but I'm not sure.
Posted

1 solution

As you said perhaps something is infact incorrect in the code above. I see that you are calling SetTemperature1(temperature) inside the foreach loop and if this is setting the same variable then the very last value will overwrite all previosu value.

For xml parsing you can also use XPath.

But I don't this xml parsing is issue here, I think calling SetTemperature1(temperature) in a loop is causing last tempetaure value to overwrite all previous value of variable that you are setting inside SetTemperature1(temperature) method.
 
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