开发者

Searching in XML using Xpath

开发者 https://www.devze.com 2022-12-29 18:37 出处:网络
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

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>
      <Manda开发者_开发技巧tory>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", "");

}


This is how I would parse your XML:

        List<String> rangeNames = new List<String>();
        XmlDocument document = new XmlDocument();
        document.Load(strConfigFile);
        XmlNodeList fieldInfoList = null;
        fieldInfoList = document.GetElementsByTagName("FieldInfo");

        foreach (XmlNode fieldInfo in fieldInfoList)
        {

            rangeNames.Add(fieldInfo["RangeName"].InnerText);

        }
0

精彩评论

暂无评论...
验证码 换一张
取 消