XDocument - not reading my XML file

Can someone tell me what I'm doing wrong here? When I run the debugger, it skips over the
 Console.WriteLine(r.TradingPartnerId); 

Open in new window

. This should not be the case, when there are nodes in the file.

      
class Program
    {
        static void Main(string[] args)
        {
            XDocument document = XDocument.Load("C:\\Order.xml");
            var orders = from r in document.Descendants("OrderHeader")
                         select new
                         {
                             TradingPartnerId = r.Element("ProductId").Value
                         };
        foreach(var r in orders)
        {
            Console.WriteLine(r.TradingPartnerId);
        }

        Console.ReadLine();
        }
    }
}

Open in new window

<?xml version="1.0" encoding="UTF-8"?>
<Orders test-inc.com">
  <Order>  
      <OrderHeader>
        <PartnerId>TEST</PartnerId>
        <PurchaseOrderNumber>01263</PurchaseOrderNumber>
        <PurchaseOrderDate>10/31/2013 AM</PurchaseOrderDate>
        <PurchaseOrderTime>10:08:00 AM</PurchaseOrderTime>
        <CustomerShippingAccount>W564</CustomerShippingAccount>
        <CustomerPONumber>test123</CustomerPONumber>
      </OrderHeader> 
  </Order>
</Orders>

Open in new window

teknovationAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Najam UddinCommented:
TradingPartnerId = r.Element("ProductId").Value

there is no Productid in xml
0
teknovationAuthor Commented:
Oops - ok, I fixed that but nothing happens still even with the values in the file. Thoughts?

Only a blank black Console window
0
teknovationAuthor Commented:
Updated and it checks if the orders is null or not. Apparently it's not null so it gets to the foreach section and skips over the Console.Writeline...weird.

     static void Main(string[] args)
        {
            XDocument document = XDocument.Load("C:\\Order.xml");
            var orders = from r in document.Descendants("OrderHeader")
                         select new
                         {
                             PartnerId = r.Element("PartnerId").Value
                         };

            if (orders != null)
            {
                foreach (var r in orders)
                {
                    Console.WriteLine(r.PartnerId.ToString());
                }
            }
        Console.ReadLine();
        }
    }
}

Open in new window

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Najam UddinCommented:
I created test code and it give correct result.

var xm = "<Orders><Order><OrderHeader><PartnerId>TEST</PartnerId>" +
                     "<PurchaseOrderNumber>01263</PurchaseOrderNumber><PurchaseOrderDate>10/31/2013 AM</PurchaseOrderDate><PurchaseOrderTime>10:08:00 AM</PurchaseOrderTime>" +
                     "<CustomerShippingAccount>W564</CustomerShippingAccount><CustomerPONumber>test123</CustomerPONumber></OrderHeader> </Order></Orders>";
            XDocument document = XDocument.Parse(xm);
            var orders = from r in document.Descendants("OrderHeader")
                         select new
                         {
                             TradingPartnerId = r.Element("PartnerId").Value
                         };
            foreach (var r in orders)
            {
                Console.WriteLine(r.TradingPartnerId);
            }

            Console.ReadLine();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Najam UddinCommented:
<Orders test-inc.com"> does not have closing tag and test-inc.com" is invalid
0
Fernando SotoRetiredCommented:
This line in the XML is mal formed
<Orders test-inc.com">

May be it should be as follows, attributes must have an = and its value needs to be quoted

<Orders test="inc.com">

Open in new window

0
teknovationAuthor Commented:
Many thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Dynamics

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.