Click here to Skip to main content
15,924,193 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I retrieve the source xml of a web page using the following code:

C#
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("url");
            using (Stream stream = request.GetResponse().GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    string response = reader.ReadToEnd();
                }
            }


Usually when working with xml files I would use the following method to read it:

XmlDocument xmlDoc = new XmlDocument();
                        XmlTextReader xmlReader = new XmlTextReader(string.Format(@"{0}\config.xml", pathToConfig));
                        xmlDoc.Load(xmlReader);
                        int NodeNum = 0;
                        if (xmlDoc.ChildNodes[1].ChildNodes.Count > 0)
                        {
                            XmlNode node = null;
                            for (int i = 0; i < xmlDoc.ChildNodes[1].ChildNodes.Count; i++)
                            {
                                node = xmlDoc.ChildNodes[1].ChildNodes[i];
                                if (node.Name.ToString() == "serverIP")
                                {
                                    checkServer = true;
                                    NodeNum = i;
                                    break;
                                }
                            }
                            if (checkServer)
                            {
                                XmlNodeList nodeList = xmlDoc.ChildNodes[1].ChildNodes[NodeNum].ChildNodes;
                                string serverID = nodeList[0].InnerText.ToString();
                            }
                        }


My question is this, how can I modify the above code to work with the returned string from the web, which is basically one long string of xml, rather than with a file. Any help would be appreciated.

It's okay I figured out how to do it I just use LoadXML of the XmlDocument rather than Load.

[edit]Added "Solved" to subject - OriginalGriff[/edit]
Posted
Updated 6-Jan-11 0:59am
v3
Comments
Espen Harlinn 6-Jan-11 6:54am    
Right ...

Answered only to remove from the unanswered list - solved by OP
 
Share this answer
 
Whatever you're doing for parsing XML isn't a good practice at all.

I would suggest you to HTML Agility pack[^], which is helpful for parsing HTML.

or alternatively see Article by Nish[^] a very well known author in Code project. See how he has parsed HTML in his code.
 
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