C#我怎么能得到的所有元素的名字从一个xml文件 [英] C# how can I get all elements name from a xml file

查看:175
本文介绍了C#我怎么能得到的所有元素的名字从一个xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个XML文件中得到的所有元素的名称,例如XML文件,

I'd like to get all the element name from a xml file, for example the xml file is,

<BookStore>
  <BookStoreInfo>
    <Address />
    <Tel />
    <Fax />
  <BookStoreInfo>
  <Book>
    <BookName />
    <ISBN />
    <PublishDate />
  </Book>
  <Book>
   ....
  </Book>
</BookStore>

我想获得BOOKNAME的元素的名称。 ISBN和PublishDate,只有那些名称,不包括BookStoreInfo和它的子节点的名称

I would like to get the element's name of "BookName". "ISBN" and "PublishDate " and only those names, not include " BookStoreInfo" and its child node's name

我试了几种方法,但不工作,我该怎么办呢?

I tried several ways, but doesn't work, how can I do it?

推荐答案

那么,与的XDocument 和LINQ到XML:

Well, with XDocument and LINQ-to-XML:

foreach(var name in doc.Root.DescendantNodes().OfType<XElement>()
        .Select(x => x.Name).Distinct())
{
    Console.WriteLine(name);
}

有许多类似的路线,虽然。

There are lots of similar routes, though.

这篇关于C#我怎么能得到的所有元素的名字从一个xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆