tags:

views:

25

answers:

1

Hello.
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:

    XDocument 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!

A: 
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.

Femaref
You are almost right. You Just need to put a dot before slash.
StuffHappens