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..
LVL 2
salan_alaniAsked:
Who is Participating?
 
Geert BormansConnect With a Mentor Information ArchitectCommented:

> 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);


     
0
 
Geert BormansInformation ArchitectCommented:
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!
0
 
salan_alaniAuthor Commented:
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..
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
salan_alaniAuthor Commented:
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..
0
 
salan_alaniAuthor Commented:
Nevermind, I figured out how to do formatting to DateTime variable.

Thanks again for your help Gertone..

Salan..
0
 
Geert BormansInformation ArchitectCommented:
welcome
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.