0

I have XML document with this structure:

<dipOrders>
    <interchangeInfo senderEdiCode="LSC58" senderEdiCodeQal="ZZ" receiverEdiCode="15274" receiverEdiCodeQal="ZZ" syntax="X12" syntaxId="X" syntaxVersion="003010"/>
    <order orderNumber="219299" orderDate="2012-12-05T00:00:00" validityDate="2012-12-05T00:00:00">
        <buyer name="LEAR MTO">
            <partyCode buyerCode="811567924"/>
        </buyer>
        <supplier name="BRIDGE OF WEIR LEATHER CO">
            <partyCode buyerCode="749630"/>
        </supplier>
        <orderConsignee name="LEAR MEXICAN SEATING CORP">
            <partyCode buyerCode="LSC59"/>
            <orderLine description="LEA DC 378 HERO 6RSB 5B8" orderNumber="246767" engineeringChangeNumber="N">
                <partyCode buyerCode="DC378105H6RSB5B8AA"/>
                <cumulativeQuantity date="2012-12-04T00:00:00" quantity="0"/>
                <orderQuantity quantity="0" commitmentLevel="Firm" timingQualifier="Weekly" shipDate="2012-12-05T00:00:00"/>
            </orderLine>
            <orderLine description="LEA DC 378 HERO 6RSB 5V0" orderNumber="246767" engineeringChangeNumber="N">
                <partyCode buyerCode="DC378105H6RSB5V0AA"/>
                <cumulativeQuantity date="2012-12-04T00:00:00" quantity="0"/>
                <orderQuantity quantity="0" commitmentLevel="Firm" timingQualifier="Weekly" shipDate="2012-12-05T00:00:00"/>
            </orderLine>

I am trying to access it with Linq to XML in .NET console application to get orderLine item details:

XDocument doc = XDocument.Load(sourceDirectory + "\\" + fileName);

                var data = from item in doc.Descendants("orderLine")
                           select new
                           {
                               orderNumber = item.Element("orderNumber").Value
                           };

                foreach (var i in data)
                {
                    Console.WriteLine(i.ToString());
                }

                Console.ReadLine();

But I get "object reference not set to an instance of an object" error. Why?

1 Answer 1

3

You're trying to get an orderNumber element from the orderLine element... but it's an attribute. It's not clear why you're then creating an anonymous type from that, by the way - or why you're using a query expression to start with. I'd write this as:

var orderNumbers = doc.Descendants("orderLine")
                      .Select(x => (int) x.Attribute("orderNumber"));
foreach (int orderNumber in orderNumbers)
{
    Console.WriteLine(orderNumber);
}
Sign up to request clarification or add additional context in comments.

1 Comment

So how would I access orderQuantity attributes for instance while creating record for each orderLine object at the same time?

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.