如何在xml中获取子节点的名称

时间:2015-12-25 08:32:38

标签: c# .net xml

我的XML看起来像这样

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

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

我找到了很多方法,我在这里找到最接近的答案

Get All node name in xml in silverlight

但它会读取所有后代,我需要来自&#34;位置&#34;得到&#34; AChau&#34;

来自&#34;位置/ AChau&#34;得到&#34; ACity&#34; &#34; BCity&#34;

来自&#34;位置/ AChau / ACity&#34;得到&#34; EHouse&#34; &#34; FHouse&#34; &#34; GHouse&#34;

如何只读取子节点?

4 个答案:

答案 0 :(得分:1)

如果您使用XElement从xml获取数据 - 那么您只需要FirstNode属性和Elements方法。

FirstNode返回元素的第一个子节点,Elements返回元素的所有直接子节点。

答案 1 :(得分:1)

假设您有一个XElement,您可以使用以下代码提取其子项的名称数组:

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

例如,这段代码包含您的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();

将分别返回以下数组:

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

答案 2 :(得分:1)

如果您始终需要根目录下的第一个节点名称,则此方法有效:

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;

答案 3 :(得分:1)

试试这个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();
        }
    }
}
​