dban00b
asked on
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.GetElementsByTag Name("gree ting");
XmlNodeList target = myXmlFile.GetElementsByTag Name("targ et");
Console.WriteLine("{0} {1}",greeting[0].InnerText , target[0].InnerText);
Thanks for your help!
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.GetElementsByTag
XmlNodeList target = myXmlFile.GetElementsByTag
Console.WriteLine("{0} {1}",greeting[0].InnerText
Thanks for your help!
<?xml version="1.0"?>
<root>
<greeting>Hello</greeting>
<target>World</target>
</root>
ASKER
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"
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);
}
}
what is the result of the myXmlFile.OuterXml after myXmlFile.Load((HttpWebRes ponse) 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("//ro ot/greetin g");
XmlNode target = myXmlFile.SelectNode("//ro ot/target" );
Console.WriteLine("{0} {1}", greeting.InnerText, target.InnerText);
if you can see your Xml in that then i will change try using following code for printing
XmlNode greeting = myXmlFile.SelectNode("//ro
XmlNode target = myXmlFile.SelectNode("//ro
Console.WriteLine("{0} {1}", greeting.InnerText, target.InnerText);
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.
dban00b, to be sure try to set breakpoints and check what xml are you getting.
I have a feeling that this ((HttpWebResponse) myRequest.GetResponse()).t oString() returns something that is not expected...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Viola! That was it, I needed to turn the response into a Stream before XmlDocument::Load could read it.
Thanks!
Thanks!
Do you need help downloading a file?
Do you need help parsing the xml string?