Advanced XPath Query, please help!

Posted on 2006-05-22
Last Modified: 2008-01-09
I have the following xml file:

<?xml version="1.0" encoding="UTF-8" ?>
<raml xmlns="raml20.xsd">
         <log dateTime="2006-05-21T18:08:17" action="created" appInfo="ActualExporter" />
      <managedObject class="ADJG" distName="WBTS-4003/WCEL-400315/ADJG-6">
         <p name="AdjgBCC">0</p>
         <p name="AdjgBCCH">512</p>
         <p name="AdjgCI">20037</p>
      <managedObject class="ADJG" distName="WBTS-3049/WCEL-304912/ADJG-11">
         <p name="AdjgBCC">7</p>
         <p name="AdjgBCCH">114</p>
         <p name="AdjgCI">10491</p>
      <managedObject class="ADJG" distName="WBTS-3050/WCEL-305013/ADJG-11">
         <p name="AdjgBCC">1</p>
         <p name="AdjgBCCH">111</p>
         <p name="AdjgCI">10112</p>

I have the following two strings variables:
Source_CI = 304912 (for example)
Target_CI = 10491 (for example)

What is the XPath query that can I use to select the first node that matches the following conditions:
1) The node name should be "managedObject"
2) The value of the attribute "class" of that node should be equals to "ADJG"
3) The value of the attribute "distName" of that node should contains (not equal) the value of "Source_ID" variable
4) The value/InnerText of the child "p" node (this child "p" node should has a "name" attribute with value of "AdjgCI") of that node should be equals to the value of the "Target_CI" variable

For the example above, the XPath query should select the second "managedObject" node, which is:
<managedObject class="ADJG" distName="WBTS-3049/WCEL-304912/ADJG-11">
   <p name="AdjgBCC">7</p>
   <p name="AdjgBCCH">114</p>
   <p name="AdjgCI">10491</p>

For more details:
I am using C# .NET 2.0, and I am planning to call the method XmlNode.SelectSingleNode(String XPath). But I don't know really what the XPath should be equal on my case.

I don't know really If this could be done in one XPath query, but I am really new in this issue and any help would be appreciated.

Question by:salan_alani
    LVL 60

    Expert Comment

    by:Geert Bormans
    Hi salan_alani,

    this can be done with the following XPath query

    //managedObject[@class='ADJG'  and contains(@distName, '3049') and (p[@name = 'AdjgCI'] = '10491')] [1]

    you can build this as a string... so you can first create this string by concatenating the variables in it

    that would be something like
    "//managedObject[@class='ADJG'  and contains(@distName, '" + Source_CI + ...

    also note that you have a namespace, so you should bind this namespace in the namespaceManager
    you can bind it to the default namespace as it is in the XML ... ""
    or you can give a prefix, eg. "x" but than your XPath should change like this

    //x:managedObject[@class='ADJG'  and contains(@distName, '3049') and (x:p[@name = 'AdjgCI'] = '10491')] [1]

    LVL 2

    Author Comment

    I am always getting a null XmlNode when calling the XmlNode.SelectSingleNode
    I did not yet know how to bind the XmlNameSpaceManager

    My code like this:

    XmlDocument XD = new XmlDocument();

    XmlNamespaceManager XNM = new XmlNamespaceManager(XD.NameTable);
    XNM.AddNamespace(string.Empty, XD.DocumentElement.NamespaceURI);            

    XmlNode XN;
    // for simplicity (this should take the first managedObject node)
    XN = XD.DocumentElement.SelectSingleNode("cmData/managedObject", XNM);
    if (XN != null)

    But I am always getting the Error message

    LVL 60

    Accepted Solution


    > XN = XD.DocumentElement.SelectSingleNode("cmData/managedObject", XNM);
    SelectSingleNode accepts one Node... so make sure it is only one
    you would also need a "//" in front of the XPath
    try this
    XN = XD.DocumentElement.SelectSingleNode("//cmData/managedObject[1]", XNM);

    if that doesn't work,
    let's hard-code it in first

    XmlNamespaceManager XNM = new XmlNamespaceManager(XD.NameTable);
    XNM.AddNamespace("x", "raml20.xsd");
    XN = XD.DocumentElement.SelectSingleNode("//x:cmData/x:managedObject[1]", XNM);

    LVL 2

    Author Comment

    Thanks Gertone,

    It works fine.. really appreciate your help..

    One last question:
    I want to make an attribute with a DateTime value like the below format:

    Is there a built-in method to change the DateTime.Now to a format like above, or I need to hard-code it by my self.

    LVL 2

    Author Comment

    Nevermind, I figured out how to do formatting to DateTime variable.

    Thanks again for your help Gertone..

    LVL 60

    Expert Comment

    by:Geert Bormans

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    jaxb jaxp sax dom 7 41
    .net c# 8 41
    JPA XML tag definition 2 47
    json format text only 4 48
    Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
    Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now