LINQ到XML来的DataGridView [英] Linq to Xml to Datagridview

查看:164
本文介绍了LINQ到XML来的DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

右键,开始发疯了这里。我有以下代码:

  VAR的查询=(从C中db.Descendants(客户)
选择c.Elements());
dgvEditCusts.DataSource = query.ToList();

在此,DB涉及XDocument.Load电话。 ?我怎样才能获取数据到DataGridView的



只是觉得我应该提到:它返回一个完全空白DGV



这并不是说XML应该的问题太多了,但这里有一个例子:

 <根和GT; 
<客户>
< CUSTOMERNUMBER> 1 LT; / CUSTOMERNUMBER>
< EntryDate> 2010-04-13T21:59:46.4642 + 01:00< / EntryDate>
<名称>客户名称< /名称>
<地址>地址< /地址>
< PostCode1> AB1< / PostCode1>
< PostCode2>&2XY LT; / PostCode2>
< TelephoneNumber> 0123456789< / TelephoneNumber>
<移动电话号码> 0123456789< /移动电话号码>
< AlternativeNumber>< / AlternativeNumber>
<&EmailAddress的GT; email@address.com< / EmailAddress的>
< /客户>
< /根>


解决方案

啊,没关系,我已经制定了回答我自己的问题最终。下面是可能存在此问题为别人的代码:在db.Descendants

  VAR的查询=从C(客户) 
选择新的
{
CUSTOMERNUMBER = Convert.ToInt32((字符串)c.Element(CUSTOMERNUMBER)。值),
名称=(字符串)c.Element( NAME)。价值
地址=(字符串)c.Element(地址)。价值
邮编=(字符串)c.Element(PostCode1)。值++ C .Element(PostCode2)值
}。
dgvEditCusts.DataSource = query.ToList();


Right, starting to go crazy here. I have the following code:

var query = (from c in db.Descendants("Customer")
                             select c.Elements());
                dgvEditCusts.DataSource = query.ToList();

In this, db relates to an XDocument.Load call. How can I get the data into the DataGridView?

Just thought I should mention: it returns a completely blank dgv.

Not that the XML should matter too much, but here's an example:

<Root>
  <Customer>
    <CustomerNumber>1</CustomerNumber>
    <EntryDate>2010-04-13T21:59:46.4642+01:00</EntryDate>
    <Name>Customer Name</Name>
    <Address>Address</Address>
    <PostCode1>AB1</PostCode1>
    <PostCode2>2XY</PostCode2>
    <TelephoneNumber>0123456789</TelephoneNumber>
    <MobileNumber>0123456789</MobileNumber>
    <AlternativeNumber></AlternativeNumber>
    <EmailAddress>email@address.com</EmailAddress>
  </Customer>
</Root>

解决方案

Ah, never mind, I've worked out the answer to my own question eventually. Here's the code for anyone else that may have this problem:

var query = from c in db.Descendants("Customer")
                            select new
                            {
                                CustomerNumber = Convert.ToInt32((string)c.Element("CustomerNumber").Value),
                                Name = (string)c.Element("Name").Value,
                                Address = (string)c.Element("Address").Value,
                                Postcode = (string)c.Element("PostCode1").Value + " " + c.Element("PostCode2").Value
                            };
                dgvEditCusts.DataSource = query.ToList();

这篇关于LINQ到XML来的DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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