Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3221
  • Last Modified:

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

0
dban00b
Asked:
dban00b
1 Solution
 
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
 
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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
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
 
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
 
dban00bAuthor Commented:
Viola! That was it, I needed to turn the response into a Stream before XmlDocument::Load could read it.

Thanks!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now