locked
C# get value of XML Element after checking value of element attribute RRS feed

  • Question

  • Hi,

    i am trying to get the value of an XML element based on the value of an attribute in the XML.

    So to make it more clear. If while parsing i found an attribute with the desired value i should get the value of the element that it appears.

    I made a lot of tests using XMLReader without success. I can get the attribute value but i cannot get to the next point of checking against the desired and pick up the element's value.

              
                string Attribute = "testing";
    
                XmlReader tr = XmlReader.Create(new StringReader(Doc));
    
                //read in a node at a time 
                while (tr.Read())
                {
    
                    //if i check for XmlNodeType.Element the tr.Value is not working
    
                    if (tr.IsStartElement())
                    {
                       
                       for (int i=0; i < tr.AttributeCount; i++)
                       {
                           // Get the value of attribute
                           string attributeValue = (string)tr.GetAttribute(i);
    
                           if (string.Compare(attributeValue, Attribute) == 0)
                           {
                               this.logger.Debug("Found: " + attributeValue);
                           }
    
                           
                       }
                           
                    }
    
                }

    Any help appreciated!

    Wednesday, August 20, 2014 9:18 AM

Answers

  • Let's say i have the following XML

    <bookstore>
    <book genre="sci-fi">Star Wars</book>
    <book genre="adventure">Bullets</book>
    </bookstore>
    I want to check attribute genre value and if value is equal to adventure i want to get the value of this element.

    Assuming this is not an enormously huge file.

     var xml = XDocument.Parse(@"<?xml version=""1.0"" ?> 
    <bookstore>
    <book genre=""sci-fi"">Star Wars</book>
    <book genre=""adventure"">Bullets</book>
    </bookstore>");
     var xs = (from el in xml.Descendants("book")
               where (string)el.Attribute("genre") == "adventure"
               select el.Value).ToList();


    Hope that helps
    Please don't forget to up vote answers you like or which help you and mark one(s) which answer your question.

    • Proposed as answer by Mr. Zator Wednesday, August 20, 2014 12:22 PM
    • Marked as answer by svizi Wednesday, August 20, 2014 2:26 PM
    Wednesday, August 20, 2014 12:10 PM
  • I'm looking at vs2013 right now.

    Did you try adding a reference?

    Right click References in the solution explorer.

    Add Reference

    Type System.xml in the Box top right.

    Choose both System.xml and System.XMl.Linq

    Then add

    using System.Xml.Linq;

    At the top of your window/whatever.


    Hope that helps
    Please don't forget to up vote answers you like or which help you and mark one(s) which answer your question.


    • Edited by Andy ONeill Wednesday, August 20, 2014 12:57 PM
    • Marked as answer by svizi Wednesday, August 20, 2014 2:26 PM
    Wednesday, August 20, 2014 12:54 PM

All replies

    • Proposed as answer by Ioana Vasilescu Wednesday, August 20, 2014 10:59 AM
    Wednesday, August 20, 2014 10:57 AM
  • Your description is pretty unclear. Can you rephrase it?
    Wednesday, August 20, 2014 11:19 AM
  • I would usually go with LINQ to xml and you can use natural syntax or a lambda to search for a node based on an attribute.

    Do you know the name of the attribute?

    And have you got a snippet of your xml.

    It's pretty easy to write linq to xml when you know how.

    Here's a flavour from a snippet I did recently:

     var lst = xml.Descendants("address_component")
     .Where(x => (string)x.Element("type") == "administrative_area_level_1" ||
     (string)x.Element("type") == "administrative_area_level_2"
     )
     .Select(x => (string)x.Element("long_name")).ToList();


    Hope that helps
    Please don't forget to up vote answers you like or which help you and mark one(s) which answer your question.

    Wednesday, August 20, 2014 11:20 AM
  • Let's say i have the following XML

    <bookstore>
    <book genre="sci-fi">Star Wars</book>
    <book genre="adventure">Bullets</book>
    </bookstore>
    I want to check attribute genre value and if value is equal to adventure i want to get the value of this element.
    Wednesday, August 20, 2014 11:45 AM
  • Andy : Your approach is good for small XML files.  When using very large XML files it is better to use a Reader like in the code I posted.

    jdweng

    Wednesday, August 20, 2014 11:54 AM
  • Hi Joel,

    what is the part of the code i should look at?

    Wednesday, August 20, 2014 12:02 PM
  • The way you are processing this XML is quite exotic, you should read some articles to improve it.

    However if you still want to go that way to get element value you must go further into symbol every time you find desired attribute value like:

                while (tr.Read())
                {
                    if (tr.IsStartElement())
                    {
                        for (int i = 0; i < tr.AttributeCount; i++)
                        {
                            // Get the value of attribute
                            string attributeValue = (string)tr.GetAttribute(i);
                            if (string.Compare(attributeValue, Attribute) == 0)
                            {
                                while (tr.Read() && !tr.HasValue) ;
                                string elementValue = tr.Value;
                                Console.WriteLine("Found: " + elementValue);
                                break;
                            }
    
                        }
    
                    }
                }

    • Proposed as answer by Mr. Zator Wednesday, August 20, 2014 12:22 PM
    Wednesday, August 20, 2014 12:04 PM
  • Let's say i have the following XML

    <bookstore>
    <book genre="sci-fi">Star Wars</book>
    <book genre="adventure">Bullets</book>
    </bookstore>
    I want to check attribute genre value and if value is equal to adventure i want to get the value of this element.

    Assuming this is not an enormously huge file.

     var xml = XDocument.Parse(@"<?xml version=""1.0"" ?> 
    <bookstore>
    <book genre=""sci-fi"">Star Wars</book>
    <book genre=""adventure"">Bullets</book>
    </bookstore>");
     var xs = (from el in xml.Descendants("book")
               where (string)el.Attribute("genre") == "adventure"
               select el.Value).ToList();


    Hope that helps
    Please don't forget to up vote answers you like or which help you and mark one(s) which answer your question.

    • Proposed as answer by Mr. Zator Wednesday, August 20, 2014 12:22 PM
    • Marked as answer by svizi Wednesday, August 20, 2014 2:26 PM
    Wednesday, August 20, 2014 12:10 PM
  • I will try this.

    On VS2013 i cannot use the System.Xml.Linq namespace for some reason.

    Wednesday, August 20, 2014 12:29 PM
  • I'm looking at vs2013 right now.

    Did you try adding a reference?

    Right click References in the solution explorer.

    Add Reference

    Type System.xml in the Box top right.

    Choose both System.xml and System.XMl.Linq

    Then add

    using System.Xml.Linq;

    At the top of your window/whatever.


    Hope that helps
    Please don't forget to up vote answers you like or which help you and mark one(s) which answer your question.


    • Edited by Andy ONeill Wednesday, August 20, 2014 12:57 PM
    • Marked as answer by svizi Wednesday, August 20, 2014 2:26 PM
    Wednesday, August 20, 2014 12:54 PM