C# XML Parsing from an Internet URL

I've gone around in circles on this one.  Hopefully someone can give me a simple Hello World code.

I want to open an XML file via the internet via something like this
(HttpWebRequest) WebRequest.Create("www.example.com/helloworld.xml");

and parse the file and output the contents.  It doesn't need to be fancy I just hope it ends up with something like this, or I've been WAY off base:

XmlNodeList greeting = myXmlFile.GetElementsByTagName("greeting");
XmlNodeList target = myXmlFile.GetElementsByTagName("target");
Console.WriteLine("{0} {1}",greeting[0].InnerText, target[0].InnerText);



Thanks for your help!
<?xml version="1.0"?>
<root>
	<greeting>Hello</greeting>
	<target>World</target>
</root>

Open in new window

LVL 1
dban00bAsked:
Who is Participating?
 
Tony McCreathTechnical SEO ConsultantCommented:
In .Net 2.0 you can't load an XmlDocument with an HttpWebResponse!

Try the following.


myXmlFile.Load(myRequest.GetResponse().GetResponseStream());

Open in new window

0
 
anarki_jimbelCommented:
What output do you want to see?

Do you need help downloading a file?
Do you need help parsing the xml string?
0
 
dban00bAuthor Commented:
I'm having trouble changing a HttpWebResponse into an XmlDocument.

Attached is the code I think should work, but doesn't.

The output I'm expecting is "Hello World"
class XmlTest
{
	static void Main(string[] args)
	{
		XmlDocument myXmlFile = new XmlDocument(); 
		HttpWebRequest  myRequest  = (HttpWebRequest) WebRequest.Create("http://www.example.com/helloworld.xml");
 
		myXmlFile.Load((HttpWebResponse) myRequest.GetResponse());
 
		XmlNodeList greeting = myXmlFile.GetElementsByTagName("greeting");
		XmlNodeList target = myXmlFile.GetElementsByTagName("target");
 
		Console.WriteLine("{0} {1}",greeting[0].InnerText, target[0].InnerText);
	}
}

Open in new window

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Anurag ThakurTechnical ManagerCommented:
what is the result of the myXmlFile.OuterXml after myXmlFile.Load((HttpWebResponse) myRequest.GetResponse()); line

if you can see your Xml in that then i will change try using following code for printing

XmlNode greeting = myXmlFile.SelectNode("//root/greeting");
XmlNode target = myXmlFile.SelectNode("//root/target");

Console.WriteLine("{0} {1}", greeting.InnerText, target.InnerText);
0
 
anarki_jimbelCommented:
No ragi0017, I believe the problem is in the response value. The dban00b's xml processing procedure is not much different from yours and is 100% correct.

dban00b, to be sure try to set breakpoints and check what xml are you getting.
0
 
anarki_jimbelCommented:
I have a feeling that this ((HttpWebResponse) myRequest.GetResponse()).toString() returns something that is not expected...
0
 
dban00bAuthor Commented:
Viola! That was it, I needed to turn the response into a Stream before XmlDocument::Load could read it.

Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.