none
XmlNode - xpath?

    Question

  • Hi, I have an XmlNode (System.Xml.XmlNode), from which I wish to select specific child nodes. 

    Like all child nodes which have specific names and specific attributes/values. For example, all child nodes which have a name in the list ["int", "float", "string"] and which have an attribute called "location" with a value in the list ["dk", "uk", "us"].

    Example nodes would be:

    <int location="uk">123</int>

    <int location="us">111</int>

    <string location="uk">abc</string>

    but not

    <int country="uk">111</int> - no "location" attribute

    <long location="uk">999</int> - "long" is not requested

    <int location="de">123</int> - "de" is not requested

    Is there an "elegant" (xpath) way of doing this - or do I need to write some loops in code?

    Thanks!

    Tuesday, May 28, 2013 11:00 AM

Answers

  • try this

    XmlDocument xml = new XmlDocument();
    xml.LoadXml(xmlinStringForm);
    
    XmlNodeList countriesXmlNodeList = xml.SelectNodes("/Root/Country[@name = 'Pakistan']");
    foreach (XmlNode xmlnode in countriesXmlNodeList)
    {
    //todo: do your work with xmlnode, that represents Country
    }


    Faisal Ahmed Farooqui —————————— If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, May 28, 2013 11:23 AM
  • I believe this is what you are looking for:

          string xml="<root><int location=\"uk\">123</int><int location=\"us\">111</int><string location=\"uk\">abc</string><int country=\"uk\">111</int><long location=\"uk\">999</long><int location=\"de\">123</int></root>";
          XDocument doc =  XDocument.Parse(xml);
          var nodes = (from node in doc.Root.Descendants()
                            where node.Attribute("location") != null
                            &&
                            (node.Attribute("location").Value.Equals("dk") || node.Attribute("location").Value.Equals("uk") || node.Attribute("location").Value.Equals("us"))
                            &&
                            (node.Name.LocalName.Equals("int") || node.Name.LocalName.Equals("float") || node.Name.LocalName.Equals("string"))
                            select node).ToList();

    Tuesday, May 28, 2013 1:25 PM

All replies

  • try this

    XmlDocument xml = new XmlDocument();
    xml.LoadXml(xmlinStringForm);
    
    XmlNodeList countriesXmlNodeList = xml.SelectNodes("/Root/Country[@name = 'Pakistan']");
    foreach (XmlNode xmlnode in countriesXmlNodeList)
    {
    //todo: do your work with xmlnode, that represents Country
    }


    Faisal Ahmed Farooqui —————————— If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, May 28, 2013 11:23 AM
  • I believe this is what you are looking for:

          string xml="<root><int location=\"uk\">123</int><int location=\"us\">111</int><string location=\"uk\">abc</string><int country=\"uk\">111</int><long location=\"uk\">999</long><int location=\"de\">123</int></root>";
          XDocument doc =  XDocument.Parse(xml);
          var nodes = (from node in doc.Root.Descendants()
                            where node.Attribute("location") != null
                            &&
                            (node.Attribute("location").Value.Equals("dk") || node.Attribute("location").Value.Equals("uk") || node.Attribute("location").Value.Equals("us"))
                            &&
                            (node.Name.LocalName.Equals("int") || node.Name.LocalName.Equals("float") || node.Name.LocalName.Equals("string"))
                            select node).ToList();

    Tuesday, May 28, 2013 1:25 PM