2

当 Databind() 调用时,它会在新客户处产生错误。请仅通过带有 lambda 的 linq 解决它。

错误 :

你调用的对象是空的

IEnumerable<Customers> Customers = XDocument.Load("Customers.xml")
    .Descendants("Customer")
        .Select(x => new Customers
        {
            Name = x.Element("Name").Value,
            City = x.Element("City").Value,
            Contact = x.Element("Contact").Value
        });

grid.DataSource = Customers;
grid.DataBind();

public class Customers
{
    public Customers() { }
    public string Name { get; set; }
    public string City { get; set; }
    public string Contact { get; set; }
}

XML:

 <?xml version="1.0" encoding="utf-8"?>
<Customers>
    <Customer>
        <Name>John</Name>
        <City>Ney York</City>
        <Contact>2233</Contact>
    </Customer>
    <Customer>
        <Name>Albert</Name>
        <City>Sydney</City>
        <Contact>4455</Contact>
    </Customer>
    <Customer>
        <Name>David</Name>
        <City>Colombo</City>
        <Contact>6677</Contact>
    </Customer>
    <Customer>
        <Name>George</Name>
        <City>London</City>
        <Contact>8899</Contact>
    </Customer>
</Customers>
4

2 回答 2

5

试试这个..

IEnumerable<Customers> Customers = XDocument.Load("Customers.xml").Element("Customers")
  .Descendants("Customer")
    .Select(x => new Customers
    {
        Name = x.Element("Name").Value,
        City = x.Element("City").Value,
        Address = x.Element("Address").Value
    });

希望这项工作。

于 2012-07-31T08:19:48.080 回答
0

用's'与客户一起修改

  List<Customers> data =   XDocument.Load("Customers.xml").Descendants("Customers")
                                    .Select(x => new Customers
                                        {
                                            Name = x.Element("Name").Value,
                                            City = x.Element("City").Value,
                                            Address = x.Element("Address").Value
                                        }).ToList();
于 2012-07-31T08:05:44.940 回答