parsing XML content – C#

I have not used XML for very long and need to extract the useful information from an XML response. If there are 2 tags that are the same but have a different name e.g

    <lst name = "stack">
       <str>Ola</str>
       <lst name = "overflow">
          <str>Hello</str>
       </lst>
     </lst>

How would I extract the contents of the tag with name=”overflow”?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can use LINQ To XML:

var result = XDocument.Parse(xml)
                .Descendants("lst")
                .Where(e => (string) e.Attribute("name") == "overflow")
                .Descendants("str")
                .Select(x => x.Value)
                .FirstOrDefault();

Method 2

Try this to start:

XPathDocument docNav = new XPathDocument(pathName);
XPathNavigator nav = docNav.CreateNavigator();
XmlNamespaceManager ns = new XmlNamespaceManager(nav.NameTable);

string val =  nav.SelectSingleNode(@"/lst/lst[@name='overflow']/str")

These are good resources for simple XPath navigation and .NET XML Parsing:

http://www.w3schools.com/xpath/

http://www.codeproject.com/Articles/52079/Using-XPathNavigator-in-C

Method 3

You may use the System.Xml.Linq namespace:

var xDoc = XDocument.Parse(xml);
var result = xDoc.Descendants()
    .Where(d => 
        d.Name == "lst" && 
        d.Attributes("name").FirstOrDefault()!=null &&
        d.Attributes("name").FirstOrDefault().Value == "overflow")
    .FirstOrDefault();

Method 4

User Linq to xml

var xmlFile = XDocument.Load(someFile);
var query = from item in xmlFile.Descendants("childobject")
            where !String.IsNullOrEmpty(item.Attribute("using")
            select new 
            {
                AttributeValue = item.Attribute("using").Value
            };

Method 5

You can do it with LINQ to XML:

var doc = XDocument.Load("YourXMLPath.xml");
var content = doc
.Element("lst")
.Elements("lst")
.Where(e=>((string) e.Attribute("name") ?? "")=="overflow")
.Select(e=>e.Element("str").InnerText())
.FirstOrDefault();

Method 6

LINQ to XML in System.Xml.Linq namespace.

const string xml = @"<lst name = ""stack""><str>Ola</str><lst name = ""overflow""><str>Hello</str></lst></lst>";

XDocument doc = XDocument.Parse(xml);

IEnumerable<XElement> overflow = doc.Root.Elements("lst").Where(x => (string) x.Attribute("name") == "overflow");

XElement firstOverflow = overflow.FirstOrDefault();

string value = firstOverflow.Descendants("str").FirstOrDefault(x => x.Value);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x