1

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.

3 Answers 3

2
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();
Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

0

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.