C # XML File Analysis

I looked at several threads here when the stack overflowed, and I can not find the answer. I have an xml file as shown below:

<entry id="1" type="a"> <name>string 1</name> <description>any description</description> </entry> <entry id="2" type="b"> <name>string 2</name> <description>any description #2</description> </entry> 

I need to select all the "entry" tags and return the identifier, Type tag, internal name and description of the entry. How can I do this using C #?

Thanks,

+8
c # xml linq
source share
2 answers

Keep in mind that your xml file must have one root root. Here is the Linq parsing in Xml:

 var xdoc = XDocument.Load(path_to_xml); var entries = from e in xdoc.Descendants("entry") select new { Id = (int)e.Attribute("id"), Type = (string)e.Attribute("type"), Name = (string)e.Element("name"), Description = (string)e.Element("description") }; 

Query will return a sequence of anonymous objects corresponding to each input element (with property identifiers, type, name and description).

+13
source share

Check out the HtmlAgilityPack library. Using it, you can parse HTML using LINQ or XPath.

+1
source share

All Articles