Link to home
Start Free TrialLog in
Avatar of salan_alani
salan_alani

asked on

Advanced XPath Query, please help!

I have the following xml file:

<?xml version="1.0" encoding="UTF-8" ?>
<raml xmlns="raml20.xsd">
   <cmData>
      <header>
         <log dateTime="2006-05-21T18:08:17" action="created" appInfo="ActualExporter" />
      </header>
      <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>
      <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>
      <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>
      </managedObject>
      .
      .
      .
      .
   </cmData>
</raml>

I have the following two strings variables:
Source_CI = 304912 (for example)
and
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>
</managedObject>


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.


Salan..
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

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]


Cheers!
Avatar of salan_alani
salan_alani

ASKER

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();
XD.Load("MySource.xml");

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)
    MessageBox.Show(XN.OuterXml);
else
    MessageBox.Show("Error");


But I am always getting the Error message


Salan..
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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:
2006-05-21T17:28:48

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.


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

Thanks again for your help Gertone..

Salan..