Abbas
Abbas

Reputation: 5044

reading xml file in asp.net

I have an xml file with the following structure:

< rewriteMaps>  
  < rewriteMap name="StaticRewrites" />  
  < add key="/superstar2011" value="/article.aspx?articleid=4014" />  
  < add key="/superstar2012" value="/article.aspx?articleid=4012" />  
  < add key="/superstar2012" value="/article.aspx?articleid=4012" />  
< /rewriteMaps> 

I have a gridview with which I want to bind the key and values. How should I go about it? I am new to xml with gridview. Any help will be greatly appreciated.

Upvotes: 1

Views: 996

Answers (3)

user2034765
user2034765

Reputation: 1

Read the xml file and collect the data in a datatable or dataset. Then bind the gridview with this datatable or dataset. Once the data is collected in a dataset or datatable, you can simply bind the gridview with the datatable or dataset via the following 2 lines:

GridView1.DataSource=ds;
GridView1.DataBind();

Upvotes: 0

Wicked Coder
Wicked Coder

Reputation: 1118

You can consider using a XMLDatasource.

<asp:xmldatasource id="XmlDataSource1" runat="server" datafile="books.xml" />

and then Bind it to control as

<asp:TreeView id="TreeView1" runat="server" datasourceid="XmlDataSource1">
        <databindings>
          <asp:treenodebinding datamember="book" textfield="title"/>
        </databindings>
</asp:TreeView>

Upvotes: 0

danyolgiax
danyolgiax

Reputation: 13086

XElement x = XElement.Parse("<rewriteMaps><rewriteMap name=\"StaticRewrites\" /><add key=\"/superstar2011\" value=\"/article.aspx?articleid=4014\" /><add key=\"/superstar2012\" value=\"/article.aspx?articleid=4012\" /><add key=\"/superstar2012\" value=\"/article.aspx?articleid=4012\" /></rewriteMaps>");

var r = from i in x.Descendants("add")
                    select new { key = "key", value = "value" };

yourGrid.Datasource = r;
yourGrid.DataBind();

or like this example:

DataSet dataSet= new DataSet();
string filePath = Server.MapPath("your.xml");

dataSet.ReadXml(filePath);                    
yourGrid.DataSource = dataSet.Tables[0].DefaultView;
yourGrid.DataBind();

Upvotes: 2

Related Questions