简体   繁体   中英

C# Read Sub Elements with XMLReader(?)

I'm currently struggeling with reading Sub-Elements from Sub-Elements. What would be the best way to read a XML structured like that:

<network>
<channel type="...">
        <sub1.1>...</sub1.1>
        <sub1.2>...</sub1.2>
        ...
</channel>

<channel type="...">
        <sub1.1>...</sub1.1>
        <sub1.2>...</sub1.2>
        ...
</channel>
...

<group number="...">
        <sub2.1>...</sub2.1>
        <sub2.2>...</sub2.2>
        ...
</group>
...

<group number="...">
        <sub2.1>...</sub2.1>
        <sub2.2>...</sub2.2>
        ...
</group>
...

<gateway id="...">
        <sub3.1>...</sub3.1>
        <sub3.2>...</sub3.2>
        <sub3.3>
                <sub3.3.1>...</sub3.3.1>
        </sub3.3>
        <sub3.4>
                <sub3.4.1>...</sub3.4.1>
        </sub3.4>
...
</gateway>
...
<network>

I would like to read the Attributes like type, number, id... and also all the sub elements and sub-sub elements. I tried it with XMLReader but struggled with getting sub-sub elements etc...

If someone could help me with getting me an approach I would be very thankful.

You can use MoveToFirstElementAttribute() and MoveToNextAttribute() to get attribute's name (and it's values)

string fileName = @"C:\file.xml";

using (XmlTextReader reader = new XmlTextReader(fileName)) //using System.Xml
        {
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Whitespace || reader.NodeType == XmlNodeType.EndElement) continue;
                Console.WriteLine("\n------------------------------------------------------------------------------");

                string message = $"Position: {reader.LineNumber},{reader.LinePosition}\tNode Type: {reader.NodeType.ToString()}\tDepth: {reader.Depth}\n";
                if (reader.Name.Trim() != "") message += $"Name: {reader.Name}\t";
                if (reader.Value.Trim() != "") message += $"Value: {reader.Value}\t";
                Console.WriteLine(message);

                // Read() method doesn't get into attribute nodes, so check them manually then get them by MoveToFirstElementAttribute() and MoveToNextAttribute() methods
                if (reader.HasAttributes)
                {
                    Console.WriteLine($"\nHas {reader.AttributeCount} Attribute(s):\n");
                    reader.MoveToFirstAttribute();

                    Console.WriteLine($"Attribute Name: {reader.Name}\tValue: {reader.Value}");
                    while (reader.MoveToNextAttribute())
                    {
                        Console.WriteLine($"Attribute Name: {reader.Name}\tValue: {reader.Value}");
                    }
                    reader.MoveToElement();
                }
            }
        }

I used XmlTextReader (which is child class of XmlReader ) because it includes line infos. if you want to use XmlReader then you can replace it with XmlTextReader (except XmlReader doesn't have LineNumber and LinePosition properties)

You can also check the "sub-ness" of an element by Depth property. Keep in mind that Depth starts from 0 (the outmost elements Depth value will be 0 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM