2

When Databind() calls, It generates error at new Customers. Please solve it only by linq with lambda.

The Error :

object reference not set to an instance of an object

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; }
}

The 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>
9
  • 1
    And what does your XML look like? Commented Jul 31, 2012 at 8:06
  • object reference not set to an instance of an object Commented Jul 31, 2012 at 8:08
  • <?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> Commented Jul 31, 2012 at 8:09
  • @Wajahat You can edit your question to add extra information you have rather than posting comments Commented Jul 31, 2012 at 8:11
  • thank you but please solve this problem as well :) Commented Jul 31, 2012 at 8:14

2 Answers 2

5

Try this..

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

Hope this work.

Sign up to request clarification or add additional context in comments.

Comments

0

Modify with Customers with '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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.