开发者

How does XPathSelectElement work? C#

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜