binding a xml file located on a website to GridView

I'm using Visual Studio 2013, Asp.net web forms website, C#.

I have a regular aspx page called Default.aspx
In my project directory I have an xml file called Employees.xml
So right now I have the following code in my Defaul.aspx.cs

On the page load I call the xml file and bind it to the GridView control. This works just fine.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class _NwndEmployees : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        // create dataset
        DataSet oDs = new DataSet();

        // request xml file for use by dataset
        oDs.ReadXml(Request.PhysicalApplicationPath + "Employees.xml");

        // set dataset as the source for the gridview
        GridView1.DataSource = oDs;

        // bind dataset to gridview
        GridView1.DataBind();
        
    }
}

Open in new window


So my question is how do I revise my example to bind the xml to gridivew if my xml file was located on some website instead of being a static xml file like my example above.

So for example if my xml file was located here:

http://www.mywebsite.com/data/Employees.xml

I'm not sure how to revise this line?:

        // request xml file for use by dataset
        oDs.ReadXml(Request.PhysicalApplicationPath + "Employees.xml");


Anyone know how I bind a website url to gridview?
LVL 1
maqskywalkerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi maqskywalker;

This should do what you need.

using System.Xml;

protected void Page_Load(object sender, EventArgs e)
{

    // Url to get the XML file from
    string url = "http://www.mywebsite.com/data/Employees.xml";

    // Create a stream to get the file from the web site
    XmlTextReader reader = new XmlTextReader(url);
    
    // create dataset
    DataSet oDs = new DataSet();

    // request xml file for use by dataset
    oDs.ReadXml(reader);

    // ======== This may need to be oDs.Tables[0] ===========
    // set dataset as the source for the gridview
    GridView1.DataSource = oDs;

    // bind dataset to gridview
    GridView1.DataBind();
    
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.