当 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>