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-4 00315/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-3 04912/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-3 05013/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-3 04912/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(S tring 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..
<?xml version="1.0" encoding="UTF-8" ?>
<raml xmlns="raml20.xsd">
<cmData>
<header>
<log dateTime="2006-05-21T18:08
</header>
<managedObject class="ADJG" distName="WBTS-4003/WCEL-4
<p name="AdjgBCC">0</p>
<p name="AdjgBCCH">512</p>
<p name="AdjgCI">20037</p>
</managedObject>
<managedObject class="ADJG" distName="WBTS-3049/WCEL-3
<p name="AdjgBCC">7</p>
<p name="AdjgBCCH">114</p>
<p name="AdjgCI">10491</p>
</managedObject>
<managedObject class="ADJG" distName="WBTS-3050/WCEL-3
<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-3
<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(S
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..
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.Nam eTable);
XNM.AddNamespace(string.Em pty, XD.DocumentElement.Namespa ceURI);
XmlNode XN;
// for simplicity (this should take the first managedObject node)
XN = XD.DocumentElement.SelectS ingleNode( "cmData/ma nagedObjec t", XNM);
if (XN != null)
MessageBox.Show(XN.OuterXm l);
else
MessageBox.Show("Error");
But I am always getting the Error message
Salan..
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.Nam
XNM.AddNamespace(string.Em
XmlNode XN;
// for simplicity (this should take the first managedObject node)
XN = XD.DocumentElement.SelectS
if (XN != null)
MessageBox.Show(XN.OuterXm
else
MessageBox.Show("Error");
But I am always getting the Error message
Salan..
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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..
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..
ASKER
Nevermind, I figured out how to do formatting to DateTime variable.
Thanks again for your help Gertone..
Salan..
Thanks again for your help Gertone..
Salan..
welcome
this can be done with the following XPath query
//managedObject[@class='AD
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='A
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='
Cheers!