AM031447
AM031447

Reputation: 495

How to get the name of the child node in xml

My XML looks like this

<Location>
    <AChau>
        <ACity>
          <EHouse/>
          <FHouse/>
          <GHouse/>
        </ACity>

        <BCity>
          <HHouse/>
          <IHouse/>
          <JHouse/>
          <KHouse/>
        </BCity>
    </AChau>
</Location>

I find a number of ways, I am here to find the closest answer

Get All node name in xml in silverlight

But it reads all the descendants, I need is from "Location" get "AChau"

From "Location/AChau" get "ACity" "BCity"

From "Location/AChau/ACity" get "EHouse" "FHouse" "GHouse"

How can I read only child node?

Upvotes: 0

Views: 3370

Answers (4)

jdweng
jdweng

Reputation: 34421

Try this xml linq

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml = 
                "<Location>" +
                    "<AChau>" +
                        "<ACity>" +
                          "<EHouse/>" +
                          "<FHouse/>" +
                          "<GHouse/>" +
                        "</ACity>" +
                        "<BCity>" +
                          "<HHouse/>" +
                          "<IHouse/>" +
                          "<JHouse/>" +
                          "<KHouse/>" +
                        "</BCity>" +
                    "</AChau>" +
                "</Location>";

            XElement location = XElement.Parse(xml);

            var results = location.Descendants("AChau").Elements().Select(x => new
            {
                city = x.Name.LocalName,
                houses = string.Join(",",x.Elements().Select(y => y.Name.LocalName).ToArray())
            }).ToList();
        }
    }
}
​

Upvotes: 1

Yeldar Kurmangaliyev
Yeldar Kurmangaliyev

Reputation: 34189

Assuming that you have an XElement, you can extract the array of names of its children using the following code:

string[] names = xElem.Elements().Select(e => e.Name.LocalName).ToArray();

For example, this code with your XML:

public static MyXExtensions 
{
    public static string[] ChildrenNames(this XElement xElem)
    {
        return xElem.Elements().Select(e => e.Name.LocalName).ToArray();
    }
}

string[] names1 = xDoc.Root.ChildrenNames();
string[] names2 = xDoc.Root.Element("AChau").ChildrenNames();
string[] names3 = xDoc.XPathSelectElement("Location/AChau/ACity").ChildrenNames();

will return the following arrays respectively:

["AChau"]
["ACity", "BCity"]
["EHouse", "FHouse", "GHouse"]

Upvotes: 1

Westerlund.io
Westerlund.io

Reputation: 2833

This works if you always want the first node name under the root:

string xml = @"<Location>
              <AChau>
                <ACity>
                  <EHouse/>
                  <FHouse/>
                  <GHouse/>
                </ACity>

                <BCity>
                  <HHouse/>
                  <IHouse/>
                  <JHouse/>
                  <KHouse/>
                </BCity>
              </AChau>
            </Location>";

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNode root = doc.DocumentElement;
XmlNode first = root.FirstChild;

Upvotes: 1

Andrey Korneyev
Andrey Korneyev

Reputation: 26846

If you're using XElement to get your data from xml - then all you need is FirstNode property and Elements method.

FirstNode returns first child node of element and Elements returns all direct child nodes of element.

Upvotes: 1

Related Questions