Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# how can I get all elements name from a xml file

Tags:

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> 

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?

like image 242
Smallville Avatar asked May 11 '09 12:05

Smallville


Video Answer


1 Answers

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.

like image 59
Marc Gravell Avatar answered Oct 06 '22 00:10

Marc Gravell