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
Release: 2025-01-30 05:01:08
Original
905 people have browsed it

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>
Copy after login

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

<code>A
  A1
  A2
B
  B1
  B2
C</code>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template