how to read nested elements using Linq to xml

Imir Hoxha

I am trying to read the nested elements in the xml below. So far I have been able to read data in chantier/data element, but now the problem lies in how can I read the data inside <questions><sitePreparation> and <ctm>? Xml file and code have been shorted a bit because they are too long. Any help is much appreciated.

<?xml version="1.0" encoding="UTF-8"?>
<Audit>
    <controls>
        <guid>
           0001
        </guid>
        <templateVersion>
                    1.0
        </templateVersion>
    </controls>
    <chantier>
        <data>
            <V2>V2</V2>
            <V3>V3</V3>
            <V3_1>V3_1</V3_1>
            <V4>V4</V4>
            <oresTiersPanel>
                <S1_2>S1_2</S1_2>
            </oresTiersPanel>
            <agentsTiersPanel>
                <S1_2_2>S1_2_2</S1_2_2>
            </agentsTiersPanel>
        </data>
        <questions>
            <sitePreparation>
                <P1_Question>P1_Q</P1_Question>
                <P6_Question>P6_Q</P6_Question>
            </sitePreparation>
            <ctm>
                <C1_Question>C1_Q</C1_Question>
                <C2_Question>C2_Q</C2_Question>
                <C2_1>C2_1</C2_1>
            </ctm>
        </questions>
    </chantier>
</Audit>

private static void ReadXml()
{
    XDocument xdoc = XDocument.Load("sipp.xml");

    if (xdoc.Root != null)
    {
        var chantier = from ch in xdoc.Root.Elements("chantier").Elements("data")
                       let agentsTiersPanel = ch.Element("agentsTiersPanel")
                       where agentsTiersPanel != null
                       select new
            {
                v2 = (string)ch.Element("V2"),
                v3 = (string)ch.Element("V3"),
                v3_1 = (string)ch.Element("V3_1"),
                v4 = (string)ch.Element("V4"),
                S1_2_2 = (string)agentsTiersPanel.Element("S1_2_2"),
                S1_2_2_1 = (string)agentsTiersPanel.Element("S1_2_2_1"),
                S1_2_3 = (string)agentsTiersPanel.Element("S1_2_3"),
                S3 = (string)ch.Element("S3"),
               S3_1 = (string)ch.Element("S3_1"),
                P1_Question = (string)ch.Element("P1_Question")
            };

        foreach (var item in chantier)
        {
            Console.WriteLine(item.v2 + " " + item.v3);
        }
    }
}
Sergey Berezovskiy

Sample of reading questions elements:

var questions = xdoc.Root.Elements("chantier")
                    .Elements("questions").FirstOrDefault();

if (questions != null)
{
    var sitePreparation = questions.Element("sitePreparation");
    if (sitePreparation != null)
    {
        Console.WriteLine((string)sitePreparation.Element("P1_Question"));
        Console.WriteLine((string)sitePreparation.Element("P6_Question"));
    }
}

If you want return P1 and P6 questions as part of your anonymous object, then keep in mind, that ch is a data element of chantier, not chantier element itself. That's why ch.Element("P1_Question") returns null. With skipping null elements query should look like:

var chantiers = 
   from chantier in xdoc.Root.Elements("chantier")
   let data = chantier.Element("data")
   let questions = chantier.Element("questions")
   where data != null && questions != null
   select new {
      V2 = (string)data.Element("V2"),
      V3 = (string)data.Element("V3"),
      V3_1 = (string)data.Element("V3_1"),
      V4 = (string)data.Element("V4"),
      S1_2_2 = (string)data.Element("agentsTiersPanel").Element("S1_2_2"),
      P1_Question = (string)questions.Element("sitePreparation")
                                     .Element("P1_Question")
   };

Output:

[
  {
     V2: "V2",
     V3: "V3",
     V3_1: "V3_1",
     V4: "V4",
     S1_2_2: "S1_2_2",
     P1_Question: "P1_Q"
  }
]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Read the XML using Linq and check if the elements exist

From Dev

LINQ to XML nested elements

From Dev

LINQ to XML nested elements query

From Dev

Linq to XML query nested elements

From Dev

LINQ to XML extract nested elements

From Dev

How to convert XML to nested Dictionary using Linq to XML?

From Dev

How to convert XML to nested Dictionary using Linq to XML?

From Dev

Querying nested elements with same name in XML with LINQ

From Dev

Querying nested elements with same name in XML with LINQ

From Dev

Read from XML using LINQ

From Dev

How to get multiple elements by name in XML using LINQ

From Dev

How to get multiple elements by name in XML using LINQ

From Dev

how to load nested XML into LINQ

From Dev

How to get XML elements with LINQ

From Dev

XML compare Elements and Values using LINQ to XML

From Dev

Cannot read XML comments using LINQ to XML

From Dev

Read XML using LINQ using formatting

From Dev

How to transform Nested XML elements to flat XML

From Dev

Parsing XML with pairs of elements using XDocument and LINQ

From Dev

Parsing XML with pairs of elements using XDocument and LINQ

From Dev

Parse nested elements in an xml using jaxb

From Dev

Parsing nested elements in an XML file using Java

From Dev

Read specific element from xml using linq

From Dev

Accessing nested elements while iterating an XML LINQ query?

From Dev

How to read nested array elements from JSON?

From Dev

read elements from an xml file using SAXReader

From Dev

LINQ to XML: How to get all elements by value

From Dev

How to iterate XML nested elements with Nokogiri in Ruby

From Dev

How to read namespaces c# LINQ to XML

Related Related

HotTag

Archive