Home >Backend Development >C++ >How Can LINQ to XML Efficiently Parse and Format Nested XML Data?

How Can LINQ to XML Efficiently Parse and Format Nested XML Data?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-30 05:01:081002browse

How Can LINQ to XML Efficiently Parse and Format Nested XML Data?

LINQ to XML: A Powerful Approach to XML Data Handling

.NET's LINQ to XML provides an elegant and efficient way to process XML data. Let's examine how to parse and format nested XML structures using C#. Consider this sample XML:

<code class="language-xml"><root><level1 name="A"><level2 name="A1"></level2><level2 name="A2"></level2></level1><level1 name="B"><level2 name="B1"></level2><level2 name="B2"></level2></level1><level1 name="C"></level1></root></code>

Our goal is to output this data in a hierarchical format:

<code>A
  A1
  A2
B
  B1
  B2
C</code>

Parsing with LINQ to XML

We begin by loading the XML using XDocument.Load(). Then, a LINQ query selects the level1 nodes and their "name" attributes:

<code class="language-csharp">XDocument xdoc = XDocument.Load("data.xml");

var level1Nodes = from level1 in xdoc.Descendants("level1")
                  select level1.Attribute("name").Value;</code>

Accessing Nested Level 2 Nodes

To access the nested level2 nodes, we use a nested loop. Inside the outer loop (iterating through level1Nodes), a second LINQ query filters level2 nodes based on their parent's "name" attribute:

<code class="language-csharp">foreach (var level1 in level1Nodes)
{
    Console.WriteLine(level1);
    var level2Nodes = from level2 in xdoc.Descendants("level2")
                      where level2.Parent.Attribute("name").Value == level1
                      select level2.Attribute("name").Value;
    foreach (var level2 in level2Nodes)
    {
        Console.WriteLine("  " + level2);
    }
}</code>

The indentation (" ") distinguishes level 2 from level 1 nodes in the output.

Complete C# Code

Here's the complete, functional code snippet:

<code class="language-csharp">using System.Xml.Linq;

public class XmlParser
{
    public static void Main(string[] args)
    {
        XDocument xdoc = XDocument.Load("data.xml");

        var level1Nodes = from level1 in xdoc.Descendants("level1")
                          select level1.Attribute("name").Value;

        foreach (var level1 in level1Nodes)
        {
            Console.WriteLine(level1);
            var level2Nodes = from level2 in xdoc.Descendants("level2")
                              where level2.Parent.Attribute("name").Value == level1
                              select level2.Attribute("name").Value;
            foreach (var level2 in level2Nodes)
            {
                Console.WriteLine("  " + level2);
            }
        }
    }
}</code>

This demonstrates the power and efficiency of LINQ to XML for parsing and formatting complex XML structures within a clean and readable C# codebase.

The above is the detailed content of How Can LINQ to XML Efficiently Parse and Format Nested XML Data?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn