如何使用XPathNavigator并返回不同的节点?
我有一个像这样的 XML 文件:
<Document>
<Tests>
<Test>
<Name>A</Name>
<Value>0.01</Value>
<Result>Pass</Result>
</Test>
<Test>
<Name>A</Name>
<Value>0.02</Value>
<Result>Pass</Result>
</Test>
<Test>
<Name>B</Name>
<Value>1.01</Value>
<Result>Fail</Result>
</Test>
<Test>
<Name>B</Name>
<Value>0.01</Value>
<Result>Pass</Result>
</Test>
</Tests>
</Document>
以及一个保存每个测试数据的类:
public class TestData
{
public string TestName {get; set;}
public int TestPositon {get; set;} //Position of Test node in XML file
public string TestValue {get; set;}
public string TestResult {get; set;}
}
现在我使用此代码将所有测试放入 List
doc = new XPathDocument(filePath);
nav = doc.CreateNavigator();
private List<TestData> GetAllTestData()
{
List<TestData> Datas = new List<TestData>();
TestData testData;
XPathNodeIterator it = nav.Select("/Document/Tests/Test/Name");
int pos = 1;
foreach(XPathNavigator val in it)
{
testData.TestPosition = pos;
testData = new TestData();
// This adds the Name, but what should I change to access Value and Result
// in the same nav ??
testData.TestName = val.Value;
Datas.Add(testData);
pos++; //Increment Position
}
return Datas;
}
因此,正如我在评论中所说, XPath 仅引用 Name
节点,如何在迭代器的单个 foreach 中获取所有 3 个节点?我的意思是如何分配这些东西:
testData.Value = ???
testData.Result = ???
谢谢!
I have a XML file like this:
<Document>
<Tests>
<Test>
<Name>A</Name>
<Value>0.01</Value>
<Result>Pass</Result>
</Test>
<Test>
<Name>A</Name>
<Value>0.02</Value>
<Result>Pass</Result>
</Test>
<Test>
<Name>B</Name>
<Value>1.01</Value>
<Result>Fail</Result>
</Test>
<Test>
<Name>B</Name>
<Value>0.01</Value>
<Result>Pass</Result>
</Test>
</Tests>
</Document>
And a class to hold data for each Test:
public class TestData
{
public string TestName {get; set;}
public int TestPositon {get; set;} //Position of Test node in XML file
public string TestValue {get; set;}
public string TestResult {get; set;}
}
Now I am using this code to put all Test's in a List<TestData>
doc = new XPathDocument(filePath);
nav = doc.CreateNavigator();
private List<TestData> GetAllTestData()
{
List<TestData> Datas = new List<TestData>();
TestData testData;
XPathNodeIterator it = nav.Select("/Document/Tests/Test/Name");
int pos = 1;
foreach(XPathNavigator val in it)
{
testData.TestPosition = pos;
testData = new TestData();
// This adds the Name, but what should I change to access Value and Result
// in the same nav ??
testData.TestName = val.Value;
Datas.Add(testData);
pos++; //Increment Position
}
return Datas;
}
So as I said in the comment, the XPath is only refering to Name
node, how can I get all 3 nodes in a single foreach for the itterator? I mean how to assign this things aswell:
testData.Value = ???
testData.Result = ???
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 XPath
选择测试节点。然后在 foreach 中使用
XPathNavigator.SelectSingleNode
:或者使用此 XPath:
它选择所有节点。
Use XPath
It selects test nodes. Then in
foreach
useXPathNavigator.SelectSingleNode
:Or use this XPath:
It selects all nodes.
这将返回所有包含测试/测试/名称的文档元素,
现在您可以确定,如果您扫描文档 - 您将拥有 3 个叶子。
this will return you all the document elements which has Tests/Test/Name inside them
now you can be sure that if you scan document - you will have the 3 leafs.