Home > Software design >  C# - Using a LINQ query to get values out of complex XML document
C# - Using a LINQ query to get values out of complex XML document

Time:06-30

<?xml version="10" encoding="utf-8"?>
<FormSet xmlns="www.googlecom">
<FormHeader>
<FormSectionA>
  <Order>
    <Order02>Valuexxx</Order02>
    <OrderSubGroup>
      <Order03>Valuexxx</Order03>
      <Order03>Valuexxx</Order03>
      <Order04>Valuexxx</Order04>
      <Order05>Valuexxx</Order05>
    </OrderSubGroup>
    <OrderSubGroup>
      <Order03>Valuexxx</Order03>
      <Order03>Valuexxx</Order03>
      <Order04>Valuexxx</Order04>
      <Order05>Valuexxx</Order05>
    </OrderSubGroup>
    <OrderSubGroup>
      <Order03>Valuexxx</Order03>
      <Order03>Valuexxx</Order03>
      <Order04>Valuexxx</Order04>
      <Order05>Valuexxx</Order05>
    </OrderSubGroup>
    <Order08>Valuexxx</Order08>
    <OrderFinalGroup>
      <Order09>Valuexxx</Order09>
      <Order10>Valuexxx</Order10>
      <Order11>Valuexxx</Order11>
    </OrderFinalGroup>   
  </Order>
</FormSectionA>
</FormHeader>
</formset>

I have been trying to do some form of a LINQ query from the above xml. The actual XML is more complex than that, but I think if I can figure out how to snag the value for Order11 at the bottom, I can figure the rest out. I have tried to create an XML tree, creating classes for the entire xml document (this became entirely too complex and I assumed I was overcomplicating it)

I landed on the following though

using System;
using System.Xml.Linq;
using System.Linq;

class test
{
static void Main()
{
    XDocument doc = XDocument.Load(@"test.xml");

    XNamespace googs = "http://www.google.com";

    string Str = (string)
    (from x in doc.Descendants(googs   "Order11")
     select x).First();

    Console.WriteLine(Str);

}
}

But when I run it, it tells me that there is no sequence there. I have also tried without referencing the namespace, but I feel like that was a step backwards.

Thank you, and I apologize for the common question, all the alternative solutions have not been working for me, and I do not know why. All I need is to be able to snag one single value from a somewhat complex XML document like this, the rest of my program will be fine. Ultimately, the value will be passed into a variable for other work to be done, but all of that is already sorted out.

The real XML document has an attribute that makes each subgroup unique, but I didnt include that because the final group is always uniquely named, and thats all I care about.

CodePudding user response:

XDocument doc = XDocument.Load("test.xml");
XNamespace googs = "www.googlecom";

Get one value:

string singleOrder = doc.Descendants().First(x => x.Name == googs   "Order11").Value;
Console.WriteLine(singleOrder);

Get all values from Order** elements:

List<string> orders = (
    from x in doc.Descendants()
    where x.Name.LocalName.StartsWith("Order") && !x.HasElements
    select x.Value
    ).ToList();

foreach (var order in orders)
    Console.WriteLine(order);

CodePudding user response:

The namespace you're using in your code XNamespace googs = "http://www.google.com" isn't the namespace defined in the XML <FormSet xmlns="wwwgooglecom">, so it makes sense that you're getting no sequence back.

  • Related