0

This is my code:

List<Customer> customersList =
                (
                    from e in XDocument.Load(file).Root.Elements("cust")
                    select new Customer
                    {
                        CustomerID = (int)e.Element("custid"),
                        FirstName = (string)e.Element("fname"),
                        LastName = (string)e.Element("lname"),
                        ShowsNumber = (int)e.Element("count_noshow"),
                        VisitNumber = (int)e.Element("count_resos"),
                        Cancellation = (int)e.Element("count_cancel"),
                    }).ToList();

I got {"Value cannot be null.\r\nParameter name: element"} exception on the customerList = (....) part of the code,

I can see the XML, it has a lot of cust nodes under the root node.

could you help me please?

Many thanks Best regards,

Update1

The XML:

<root source="ERB" versionmajor="5" versionminor="0" filetype="3PCustomer">
    <cust rid="303" custid="5" lname="Test" fname="Test" emailoptin="1" mailoptin="1" datecreated="2006-01-12T15:37:54.450" count_resos="2" count_noshow="0" count_cancel="0">
        <phone phonenumber="408.123.4567" countrycodeid="1" phonetype="Home"/>
        <custcode ccode="Vegetarian"/>
    </cust>
    <cust rid="303" custid="6" lname="John" fname="Doe" email="[email protected]" emailoptin="0" mailoptin="1" cnotes="Half caf double de-caf, with a twist..." datecreated="2006-01-12T17:09:57.013" count_resos="2" count_noshow="0" count_cancel="0">
        <phone phonenumber="456.456.4565" countrycodeid="1" phonetype="Home"/>
        <custcode ccode="Guest Share"/>
    </cust>
    <cust rid="303" custid="7" webcustid="1654438" lname="doe" fname="john" emailoptin="1" mailoptin="1" datecreated="2006-01-13T11:04:25.653" count_resos="1" count_noshow="0" count_cancel="0">
        <phone phonenumber="123.456.7890" countrycodeid="1" phonetype="Home"/>
    </cust>
    <!-- (...) -->
</root>
6
  • Can you show the XML? We can't help you without it. It might be a namespace issue. Commented Nov 20, 2014 at 17:06
  • how about pasting a small snippet of the XML file that you are reading from so that we can gain a visual of what you are seeing Commented Nov 20, 2014 at 17:07
  • @MarcinJuraszek Okay,I will upload it. Commented Nov 20, 2014 at 17:07
  • One propable cause of the error you are getting is that one of the 'int' child elements of cust is empty Commented Nov 20, 2014 at 17:07
  • @DJKRAZE I updated the question with the xml Commented Nov 20, 2014 at 17:09

1 Answer 1

2

You are selecting Element(), but custid, fname, etc.are not elements rather they are attributes.

Update it like this:-

List<Customer> customersList =
                (
                    from e in XDocument.Load(file).Root.Elements("cust")
                    select new Customer
                    {
                        CustomerID = (int)e.Attribute("custid"),
                        FirstName = (string)e.Attribute("fname"),
                        LastName = (string)e.Attribute("lname"),
                        ShowsNumber = (int)e.Attribute("count_noshow"),
                        VisitNumber = (int)e.Attribute("count_resos"),
                        Cancellation = (int)e.Attribute("count_cancel"),
                    }).ToList();
Sign up to request clarification or add additional context in comments.

1 Comment

exactly. It works !! :) +1 I will accept your answer when the system allows

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.