Click here to Skip to main content
13,287,116 members (57,218 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 31 Oct 2011

Select an XML attribute "and" another attribute using xpath (without Linq)

, 6 Nov 2011
Rate this:
Please Sign up or sign in to vote.
Select an XML attribute "and" another attribute using xpath.
How do we select all blingy wheels with a 5 bolt pattern using a XPath Statement?

(*Without linq*)

       <Wheel brand="HRE" boltPattern="5" Bling="True"></Wheel>
       <Wheel brand="stockHonda" boltPattern="4" Bling="False"></Wheel>
       <Wheel name="MOMO" boltPattern="4" Bling="True"></Wheel>
       <Wheel name="SomeCrapRim" boltPattern="5" Bling="false"></Wheel>

Select( xmlString, new string[] { "Cars/Wheel[@boltPattern='5']", "node()/Wheel[@Bling='True']"});

Note: This is not optimized code, it is example code. There is no need to load up the document over and over.

/// <summary>
    /// To combine multiple select statements to parse XML data
    /// This allows you to do statements such as:
    ///     get all elements where attributeA ="X" and attributeB ="Y"
    ///     Select( new string()[] { "node()/[@attributeA='X']",
    ///                              "node()/[@attributeB='Y']"}
    /// These statements are impossible in one single xPathSyntax
    /// but using this we can combine them.
    /// notes:
    ///  -Be aware that after a select parent element names
    ///   will be truncated and only one "root" element will remain.
    ///   Therefore you must use "node()" for all the select statements
    ///   except for the first one.
    ///   -To do an "or" operation you can use "node()/elementX | node()/elementY"
    ///   which would effectively return both elements.
    ///   -select examples:
    ///   -testing tool
    /// </summary>
    /// <param name="xmlSource"></param>
    /// <param name="xPaths"></param>
    /// <returns></returns>
    public static string Select(string xmlSource, string[] xPaths)
        string xmlresult = xmlSource;
        for (int i = 0; i < xPaths.Length; i++)
            xmlresult = Select(xmlresult, xPaths[i]);
        return xmlresult;
    /// <summary>
    /// Returns all subsequent nodes of an xPath select statement in a single XML string.
    /// The returned string wraps the elements in a "root" element.
    /// This is useful for parsing XML elements.
    /// notes: - This is not optimized. Use for UI only atm.
    /// </summary>
    /// <param name="xmlSource"></param>
    /// <param name="xPath"></param>
    /// <returns></returns>
    public static string Select(string xmlSource, string xPath)
        if (xmlSource.Length == 0)
            return String.Empty;
        XmlDocument doc = new XmlDocument();
        XPathNavigator nav = doc.CreateNavigator();
        XPathNodeIterator iter1 = nav.Select(xPath);
        if (iter1.Count == 0)
            return String.Empty;
        StringBuilder xml = new StringBuilder();
        while (iter1.MoveNext())
        return xml.ToString();


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
Canada Canada
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralReason for my vote of 1 It seems that the author doesn't kno... Pin
ideafixxxer7-Nov-11 22:59
memberideafixxxer7-Nov-11 22:59 
GeneralCan't you just do this: "Cars/Wheel[@boltPattern='5' and @Bl... Pin
AspDotNetDev7-Nov-11 7:51
protectorAspDotNetDev7-Nov-11 7:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 6 Nov 2011
Article Copyright 2011 by rj45
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid