开发者

How does XPathSelectElement work? C#

开发者 https://www.devze.com 2023-01-06 08:54 出处:网络
I ran into the following problem. I have an xml-file like this: <Source Name =\"ClassificationManager\">

I ran into the following problem. I have an xml-file like this:

<Source Name ="ClassificationManager">
  <Table Name ="PositiveRegex">
    <Row>
      <Field Name="RegexPattern">^.*$</Field>
      <Field Name="Fraction">5.1</Field>
      <Field Name="ClassificationTypeNumber">1</Field>
    </Row>
    <Row>
      <Field Name="RegexPattern">^.*$</Field>
      <Field Name="Fraction">0.1</Field>
      <Field Name="ClassificationTypeNumber">2</Field>
    </Row>
  </Table>

  <Table Name ="NegativeRegex">
    <Row>
      <Field Name="RegexPattern">^.*$</Field>
      <Field Name="ClassificationTypeNumber">1</Field>
    </Row>
    <Row>
      <Field Name="RegexPattern">^.*$</Field>
      <Field Name="ClassificationTypeNumber">2</Field>
    </Row>
  </Table>

</Source>

and C# program:

    XD开发者_如何转开发ocument doc = XDocument.Load(@"C:\Repository.xml");

    XElement element = doc.XPathSelectElement(@"//Source[@Name='ClassificationManager']/Table[@Name='NegativeRegex']");

    foreach (var xmlRow in element.Elements("Row"))
    {
        Console.WriteLine(xmlRow.XPathSelectElement(@"//Field[@Name='ClassificationTypeNumber']").Value);
    }

The output is 1, 1 but not 1, 2 as I want it to be. What's wrong? How to modify the C# code to get what I need?

Thank you for your help!


Console.WriteLine(xmlRow.XPathSelectElement(@"//Field[@Name='ClassificationTypeNumber']").Value);

has to be:

Console.WriteLine(xmlRow.XPathSelectElement(@"/Field[@Name='ClassificationTypeNumber']").Value);

// selects from the root, not the current element.

0

精彩评论

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

关注公众号