LINQ to xml在ASP.NET的网格视图中的结果 [英] result of linq to xml in grid view of ASP.NET

查看:60
本文介绍了LINQ to xml在ASP.NET的网格视图中的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我已经在ASP.NET中使用过linq到xml,如下所示,我希望在单独(2)列中的网格视图中产生结果:

1位作者
2-标题

但是我的结果是在下面的一列(连接的)中,例如下面的内容:

"Don Box Essential .NET"


最好的问候.


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

    }


}

推荐答案

可能是因为与GridView模式不匹配.如果共享GridView1的数据模板,将很容易.
It might be becaz of mismatch with GridView pattern. It would be easy if you share the data template of GridView1.


您可以使用此...


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


在这里,您将获得无法绑定到DatGrid的WhereSelectEnumerableIterator

Here you are getting a WhereSelectEnumerableIterator which can''t be bound to the DatGrid

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



DataGrid需要一个IEnumerable,可以使用ToList获得它.



The DataGrid needs an IEnumerable which can be obtained with a ToList

var titles = (from book in x.Descendants(&quot;book&quot;)
                     select book).ToList();



当然,您需要DataGrid具有正确的列.



Of course you need the DataGrid to have the correct columns.


这篇关于LINQ to xml在ASP.NET的网格视图中的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆