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

Keywords: xml github Windows encoding

  • .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 xle = file.Root;

            IEnumerable<XElement> ixt = xle.Elements();

            foreach (var item in ixt)
            {
                Console.WriteLine(item.Name);
            }

            Console.ReadKey();
        }
    }
}

result

taoists
buddhism

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.

Posted by ShashidharNP on Fri, 08 Nov 2019 13:08:17 -0800