How can I in C# call a URL which gives me a xml file, and then process that xml file for example for parsing.
Asked
Active
Viewed 7,471 times
2 Answers
5
To download an XML file onto your hard disk you can simply do.
XDocument doc = XDocument.Load(url);
doc.Save(filename);
How you parse it is a different matter and there are several different ways to do it. Here is a SO question that covers the subject. You can also checkout the LINQ to XML reference on MSDN.

Community
- 1
- 1

Alex McBride
- 6,881
- 3
- 29
- 30
1
using System;
using System.IO;
using System.Net;
using System.Text;
...
public static void GetFile
(
string strURL,
string strFilePath
)
{
WebRequest myWebRequest = WebRequest.Create(strURL);
WebResponse myWebResponse = myWebRequest.GetResponse();
Stream ReceiveStream = myWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader( ReceiveStream, encode );
string strResponse=readStream.ReadToEnd();
StreamWriter oSw=new StreamWriter(strFilePath);
oSw.WriteLine(strResponse);
oSw.Close();
readStream.Close();
myWebResponse.Close();
}
from : http://zamov.online.fr/EXHTML/CSharp/CSharp1.html
XML Parser:
Just pass the stream to the XML Parser.
-
assumes the url is a XML/XHTML. If not you will need to try/catch when you parse. – Oct 28 '10 at 23:54