How to get XML data from URL in c#

Original Post: https://nishantwork.wordpress.com/2012/10/10/how-to-get-xml-data-from-url-in-c/

For retrieving value from url and save as xml in format can be done by c#. I have given below code that will take website url as input parameter and return in xml form data .

Make Sure you have used: Used name space: using System.Net;

/// <summary>
///  Get Data in xml format by url
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
publicstaticXmlDocument GetXmlDataFromUrl(stringurl)
{
    //requesting the particular web page
    varhttpRequest = (HttpWebRequest)WebRequest.Create(url);
    //geting the response from the request url
    varresponse = (HttpWebResponse)httpRequest.GetResponse();
    //create a stream to hold the contents of the response (in this case it is the contents of the XML file
    varreceiveStream = response.GetResponseStream();
    //creating XML document
    varmySourceDoc = newXmlDocument();
    //load the file from the stream
    mySourceDoc.Load(receiveStream);
    //close the stream
    receiveStream.Close();
    returnmySourceDoc;
}
Display Stream to Gridview:
protected void ReadInput(object sender, EventArgs e)
{
    // Input file name.
    string fileName = Path.Combine(this.Request.PhysicalApplicationPath,
 Settings.Default.InputFileName);
 
    // Delete contents of the temporary directory.
    XlsxRW.DeleteDirectoryContents(tempDir);
 
    // Unzip input XLSX file to the temporary directory.
    XlsxRW.UnzipFile(fileName, tempDir);
 
    IList<string> stringTable;
    // Open XML file with table of all unique strings used in the workbook..
    using (var stream = new FileStream(Path.Combine(tempDir, 
@"xlsharedStrings.xml"),
        FileMode.Open, FileAccess.Read))
        // ..and call helper method that parses that XML and 
returns an array of strings.
        stringTable = XlsxRW.ReadStringTable(stream);
 
    // Get DataTable from session variable.
    var data = (DataTable)Session["data"];
    // Open XML file with worksheet data..
    using (var stream = new FileStream(Path.Combine(tempDir,
 @"xlworksheetssheet1.xml"),
        FileMode.Open, FileAccess.Read))
        // ..and call helper method that parses that XML and 
fills DataTable with values.
        XlsxRW.ReadWorksheet(stream, stringTable, data);
 
    // Update GridView.
    this.SetDataBinding();
}

As always in programming, there is more than one method to achieve the same thing.

protected void WriteOutput(object sender, EventArgs e)
{
    // Output file name.
    string fileName = Path.Combine(this.Request.PhysicalApplicationPath, 
Settings.Default.OutputFileName);
 
    // Delete contents of the temporary directory.
    XlsxRW.DeleteDirectoryContents(tempDir);
 
    // Unzip template XLSX file to the temporary directory.
    XlsxRW.UnzipFile(templateFile, tempDir);
 
    // We will need two string tables; a lookup IDictionary<string, int> 
for fast searching and
    // an ordinary IList<string> where items are sorted by their index.
    IDictionary<string, int> lookupTable;
 
    var data = (DataTable)Session["data"];
 
    // Call helper methods which creates both tables from input data.
    var stringTable = XlsxRW.CreateStringTables(data, out lookupTable);
 
    // Create XML file..
    using (var stream = new FileStream(Path.Combine(tempDir, 
@"xlsharedStrings.xml"),
        FileMode.Create))
        // ..and fill it with unique strings used in the workbook
        XlsxRW.WriteStringTable(stream, stringTable);
 
    // Create XML file..
    using (var stream = new FileStream(Path.Combine(tempDir, 
@"xlworksheetssheet1.xml"),
        FileMode.Create))
        // ..and fill it with rows and columns of the DataTable.
        XlsxRW.WriteWorksheet(stream, data, lookupTable);
 
    // ZIP temporary directory to the XLSX file.
    XlsxRW.ZipDirectory(tempDir, fileName);
 
    if (this.downloadFileCheckBox.Checked)
    {
        // Stream XLS to browser.
        this.Response.Clear();
        this.Response.ContentType = "application/vnd.ms-excel";
        this.Response.AddHeader("Content-Disposition", "attachment; 
filename=Out.xlsx");
        this.Response.WriteFile(fileName);
        this.Response.End();
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s