views:

63

answers:

2

Given this XML, I want to retrieve a XmlNodeList of <member> nodes from <group> nodes whose <id> matches 'Tech'. The matching should be case-insensitive.

   <groups>
        <group>
            <id>Tech</id>
            <members>
                <member>johndoe</member>
                <member>janedoe</member>
                <member>robdoe</member>
            </members>
        </group>
        <group>
            <id>Support</id>
            <members>
                <member>johnfoe</member>
                <member>janefoe</member>
                <member>robfoe</member>
            </members>
        </group>
    </groups>

I've tried this, expecting to get the "doe members", but it doesn't seem to return any results.

     // assume groupName='tech'
    _document.SelectNodes("//groups/group[translate(id,'abcdefghijklmnopqrstuvxyz','ABCDEFGHIJKLMNOPQRSTUVXYZ')='" + groupName.ToUpper() + "']/members/member");
A: 

I suspect the value of groupName is not what you think it is.

I compiled this example, based off your code, and I got the results you seem to be expecting:

var doc = new XmlDocument();
doc.LoadXml(
@"<groups>
    <group>
        <id>Tech</id>
        <members>
            <member>johndoe</member>
            <member>janedoe</member>
            <member>robdoe</member>
        </members>
    </group>
    <group>
        <id>Support</id>
        <members>
            <member>johnfoe</member>
            <member>janefoe</member>
            <member>robfoe</member>
        </members>
    </group>
</groups>");
foreach (XmlElement node in doc.SelectNodes("//groups/group[translate(id,'abcdefghijklmnopqrstuvxyz','ABCDEFGHIJKLMNOPQRSTUVXYZ')='" + "tech".ToUpper() + "']/members/member"))
    Console.WriteLine(node.InnerText);

johndoe
janedoe
robdoe
Press any key to continue . . .

My other guess (a stab in the dark, since it seems to work for me!), is that you are running into issues with current language (use of translate?), or the XML document you are loading is somehow broken (byte order mark?). Basically, problems with your character streams.

Merlyn Morgan-Graham
A: 

Once again, most probably your XML document is not what you've shown us. Very often the reason for a seemingly correct XPath expression not to work is that the document has a default namespace defined and the names used in the expressions are non-prefixed.

This C# console application:

using System;
using System.Xml;

class TestXPath
{
    static void Main(string[] args)
    {
        string xmlText =
@"<groups>
    <group>
        <id>Tech</id>
        <members>
            <member>johndoe</member>
            <member>janedoe</member>
            <member>robdoe</member>
        </members>
    </group>
    <group>
        <id>Support</id>
        <members>
            <member>johnfoe</member>
            <member>janefoe</member>
            <member>robfoe</member>
        </members>
    </group>
</groups>";

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xmlText);

        string groupName = "tech";

        string xpathExpr =
@"//groups/group
            [translate(id,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')
            ='"
            + groupName.ToUpper() +@"'
            ]
             /members/member";

        XmlNodeList selection = doc.SelectNodes(xpathExpr);

        foreach (XmlNode node in selection)
        {
            Console.WriteLine(node.OuterXml);
        }

    }
}

when run, produces the expected, correct result:

<member>johndoe</member>
<member>janedoe</member>
<member>robdoe</member>
Dimitre Novatchev