C × XML base XElement Name the secondary child node name of the root node

  • .NET Framework : 4.7.2
  •        IDE : Visual Studio Community 2019
  •         OS : Windows 10 x64
  •     typesetting : Markdown
  •         blog : blog.csdn.net/yushaopu
  •        github : github.com/GratefulHeartCoder

xml file

<?xml version="1.0" encoding="utf-8"?>
<cultures>
  <taoists count="100" comment="good-t">
    <book>
      <name>The Scripture of Ethics</name>
      <author>Lao Tzu</author>
    </book>
    <book>
      <name>Wen Cheng Jing Jing</name>
      <author>Guan Yin Zi</author>
    </book>
  </taoists>
  <buddhism count="321" comment="good-d">
    <book>
      <name>Diamond Sutras</name>
      <author>Shakya Muni</author>
    </book>
    <book>
      <name>Platform Sutra</name>
      <author>Hui Neng</author>
    </book>
  </buddhism>
</cultures>

code

using System;
using System.Collections.Generic;
using System.Xml.Linq;

namespace ConsoleApp
{

    class Program
    {
        static void Main(string[] args)
        {
            // The format of xml file must be correct
            XDocument file = XDocument.Load("MyXMLFile.xml");

            // Get the root node of the xml file
            XElement xmlRoot = file.Root;

            // Get the first level child node under the root node
            IEnumerable<XElement> nodeFirst = xmlRoot.Elements();

            foreach (var item in nodeFirst)
            {
                Console.WriteLine("+");

                // nodesecond is a child of the first level child
                var nodeSecond = item.Elements();
                foreach (var node in nodeSecond)
                {
                    Console.WriteLine(" -");
                    Console.WriteLine(node.Name);
                }
            }

            Console.ReadKey();
        }
    }
}

result

+
 -
book
 -
book
+
 -
book
 -
book

resource

I'm grateful to those who have helped me.
XML, DTD, XML schema, XSL, SVG can be understood properly.
C. excellent, worth learning. . NET Core has the ability of cross platform, which is worthy of attention.
Console,WinForm,WPF,ASP.NET , Azure WebJob, WCF, Unity3d, UWP can be properly understood.
Note: This is a self-study note, the quality of the inferior, so think twice before you go. When a novice comes here, he can't copy it. He should first study its image number, and when he can change it, he will naturally jump out of the pit.

Tags: xml github Windows encoding

Posted on Fri, 08 Nov 2019 10:04:15 -0500 by Hallic7