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?
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.
精彩评论