views:

23

answers:

2

How can I in C# call a URL which gives me a xml file, and then process that xml file for example for parsing.

A: 
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:

http://www.c-sharpcorner.com/uploadfile/shehperu/simplexmlparser11292005004801am/simplexmlparser.aspx

Just pass the stream to the XML Parser.

Shiftbit
assumes the url is a XML/XHTML. If not you will need to try/catch when you parse.
Shiftbit
+1  A: 

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.

Fara