XDocument doesnt read. C# - HELP!!!!!!!!!!!

Does anyone know why if I add this
xmlns="http://test-inc.com"

Open in new window


the c# code doesnt read it. But if I remove the xmlns="http://test-inc.com" from <Orders xmlns="http://test-inc.com">, it works fine.

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

Open in new window



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

        }

Open in new window

teknovationAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Zac HarrisSystems Administrator Commented:
You have too many <Orders> opening tags in your XML code remove that and see if it corrects the issue. The issue with the extra tag is the XML is not properly formed and cannot be "validated".
teknovationAuthor Commented:
if i remove the xmlns="http://test.inc.com" from Orders it works fine. The tags are properly closed.
Zac HarrisSystems Administrator Commented:
Your code:
<?xml version="1.0" encoding="UTF-8"?>
<Orders xmlns="http://test-inc.com">
<Orders>
  <Order> 
    <Header>
      <OrderHeader>
        <PartnerId>TEST</PartnerId>
        <PurchaseOrderNumber>01-F5K2</PurchaseOrderNumber>
        <PurchaseOrderDate>10/31/2013 10:08:00 AM</PurchaseOrderDate>
        <PurchaseOrderTime>10:08:00 AM</PurchaseOrderTime>
        <CustomerShippingAccount>WX4</CustomerShippingAccount>
        <CustomerPONumber>test123</CustomerPONumber>
      </OrderHeader>
      </Header>
  </Order>
</Orders>

Open in new window


Correct Code:
<?xml version="1.0" encoding="UTF-8"?>
<Orders xmlns="http://test-inc.com">
  <Order> 
    <Header>
      <OrderHeader>
        <PartnerId>TEST</PartnerId>
        <PurchaseOrderNumber>01-F5K2</PurchaseOrderNumber>
        <PurchaseOrderDate>10/31/2013 10:08:00 AM</PurchaseOrderDate>
        <PurchaseOrderTime>10:08:00 AM</PurchaseOrderTime>
        <CustomerShippingAccount>WX4</CustomerShippingAccount>
        <CustomerPONumber>test123</CustomerPONumber>
      </OrderHeader>
      </Header>
  </Order>
</Orders>

Open in new window


You have too many opening <Orders> tags...
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

teknovationAuthor Commented:
Sorry, i must have pasted it incorrectly. That's what I have in my file.

But if i remove the xmlns, then it works fine.

I wonder if this has anything to do with XNamespace. I googled and a couple things came up.
Najam UddinCommented:
Check document.Descendants you will get idea
Zac HarrisSystems Administrator Commented:
I can't tell from the code you have provided if you have  a default namespace URI already declared to all elements. If so, you need to specify that namespace too. Fortunately, LINQ to XML makes that really simple.
Najam UddinCommented:
XNamespace ns = "http://test-inc.com";
            XDocument document = XDocument.Load(@"C:\\tempaa.xml");
            var orders = (from r in document.Descendants(ns + "OrderHeader")
                         select new
                         {
                             PartnerId = r.Element(ns + "PartnerId").Value
                         }).ToList();
            foreach (var r in orders)
            {
                Console.WriteLine(r.PartnerId);
            }

            Console.ReadLine();

Open in new window

teknovationAuthor Commented:
Hi Najam, that didnt work.
Najam UddinCommented:
I tested it before giving. What is not working ?
teknovationAuthor Commented:
nothing appears on the console window. just a blank screen
Zac HarrisSystems Administrator Commented:
Are you getting any build errors?
teknovationAuthor Commented:
nope
teknovationAuthor Commented:
i stepped thru the code and it starts the for loop and then skips the console.writeline part
Zac HarrisSystems Administrator Commented:
Can you post your complete code? It's interesting that it doesn't work when you add that one thing to the code but it does happen sometimes.
teknovationAuthor Commented:
when i remove that part it works and when i dont it doesnt work

[code]    class Program
    {
        static void Main(string[] args)
        {

            XDocument document = XDocument.Load(@"C:\\PO.xml");
            XNamespace ns ="http://www.test-inc.com/";
            var orders = (from r in document.Descendants(ns + "OrderHeader")

                          select new
                          {
                              PartnerId = r.Element(ns + "PartnerId").Value
                          }).ToList();
            foreach (var r in orders)
            {
                Console.WriteLine(r.PartnerId);
            }

            Console.ReadLine();

        }
    }
}[/code]
Najam UddinCommented:
please also share PO.xml
teknovationAuthor Commented:
<?xml version="1.0" encoding="UTF-8"?>
<Orders xmlns="www.test-inc.com">
  <Order>
    <Meta>
      <Version>testVersion</Version>
    </Meta>
    <Header>
      <OrderHeader>
        <PartnerId>test</PartnerId>
        <PurchaseOrderNumber>01263-F5</PurchaseOrderNumber>
        <PurchaseOrderDate>10/31/2013 10:08:00 AM</PurchaseOrderDate>
        <PurchaseOrderTime>10:08:00 AM</PurchaseOrderTime>
        <CustomerShippingAccount>W564</CustomerShippingAccount>
        <CustomerPONumber>test123</CustomerPONumber>
      </OrderHeader>
    </Header>
  </Order>
</Orders>

Open in new window

Najam UddinCommented:
You changed the xml format, earlier it had orders under orders, namespace is different.

 for current xml

XNamespace ns = "www.test-inc.com";
            XDocument document = XDocument.Load(@"C:\\tempaa.xml");
            var orders = (from r in document.Descendants(ns + "OrderHeader")
                         select new
                         {
                             PartnerId = r.Element(ns + "PartnerId").Value
                         }).ToList();
            foreach (var r in orders)
            {
                Console.WriteLine(r.PartnerId);
            }

            Console.ReadLine();

Open in new window


Also I would recommend to prefix http:// in namespace in xml as well as code

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
teknovationAuthor Commented:
it worked thanks!
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.