Searching in XML using Xpath
- by Sathish
I have an XML file like below , using xpath and xnavigator how can get the Value of the supplied Tag Attribute for example if i supply RangeName i should get AssumptClient
<Validations>
<FieldInfo id="1">
<Name>OMID</Name>
<Mandatory>Yes</Mandatory>
<RangeName>AssumptOMID</RangeName>
<DataType>int</DataType>
<MaxLength>10</MaxLength>
</FieldInfo>
<FieldInfo id="2">
<Name>ClientName</Name>
<Mandatory>Yes</Mandatory>
<RangeName>AssumptClient</RangeName>
<DataType>string</DataType>
<MaxLength>50</MaxLength>
</FieldInfo>
<FieldInfo id="3">
<Name>OppName</Name>
<Mandatory>Yes</Mandatory>
<RangeName>AssumptProjectName</RangeName>
<DataType>string</DataType>
<MaxLength>50</MaxLength>
</FieldInfo>
<FieldInfo id="4">
<Name>OperatingGroup</Name>
<Mandatory>Yes</Mandatory>
<RangeName>AssumptOperatingGroup</RangeName>
<DataType>string</DataType>
<MaxLength>50</MaxLength>
</FieldInfo>
</Validations>
for now i am using the below code
XPathDocument doc;
XPathNavigator nav;
XPathExpression expr;
XPathNodeIterator iterator;
doc = new XPathDocument(strConfigFile);
nav = doc.CreateNavigator();
expr = nav.Compile("/configuration/Validations/FieldInfo[RangeName='AssumptClient']");
iterator = nav.Select(expr);
if (iterator.MoveNext())
{
XPathNavigator nav2 = iterator.Current.Clone();
textBox1.Text = nav2.GetAttribute("RangeName", "");
}