Click here to Skip to main content
15,912,457 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I have used linq to xml in ASP.NET like below, I like to have result in grid view in separate (2) columns:

1- author
2- title

but my result is in one column like below (Concatenated) for example like below:

“Don Box Essential .NET”


Best regards.


public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        XElement x = XElement.Parse(
@"<books>
<book>
<author>Don Box</author>
<title>Essential .NET</title>
</book>
<book>
<author>Martin Fowler</author>
<title>Patterns of Enterprise Application Architecture</title>
</book>
</books>", LoadOptions.PreserveWhitespace);

        var titles = from book in x.Descendants("book")
                     select book;

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

    }

}
Posted
Updated 11-Nov-11 20:30pm
v2
Comments
[no name] 12-Nov-11 2:32am    
FORMAT your code snippets when posting. There are instructions and I'm sure you have seen others do it, why do you ignore it?
masoud_sedighy 12-Nov-11 3:39am    
sorry i did not about instruction so me just copied and pasted from microsoft word. in future i will take care about that.
also i like to know where i can find the instruction about format.
thanks in advanced.

It might be becaz of mismatch with GridView pattern. It would be easy if you share the data template of GridView1.
 
Share this answer
 
Comments
masoud_sedighy 12-Nov-11 8:33am    
i just drag grid view1 to my default.aspx and make load page event like below, i do not know what changes i have to do in grid view.
thanks.

<asp:GridView ID="GridView1" runat="server">
you can use this...


var titles = from book in x.Descendants("book")
                    select book;

       DataTable _dt = new DataTable();
       _dt.Columns.Add("Title", typeof(string));
       _dt.Columns.Add("Autor", typeof(string));
       foreach (var b in titles)
       {
           DataRow _dr = _dt.NewRow();
           _dr["Title"] = b.Element("title").Value;
           _dr["Autor"] = b.Element("author").Value;
           _dt.Rows.Add(_dr);
       }

       GridView1.DataSource =_dt;

       GridView1.DataBind();
 
Share this answer
 
v3
Comments
[no name] 12-Nov-11 15:54pm    
FORMAT your code snippets!!
[no name] 12-Nov-11 16:05pm    
Unnecessary to create a DataTable and iterate through the collection to add rows to it. This is not very efficient. See below for correct method.
Here you are getting a WhereSelectEnumerableIterator which can't be bound to the DatGrid

C#
var titles = from book in x.Descendants("book")
                     select book;


The DataGrid needs an IEnumerable which can be obtained with a ToList
C#
var titles = (from book in x.Descendants(&quot;book&quot;)
                     select book).ToList();


Of course you need the DataGrid to have the correct columns.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900