如何使用XPathNavigator并返回不同的节点?

发布于 2024-12-15 16:26:06 字数 1937 浏览 0 评论 0原文

我有一个像这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揽月 2024-12-22 16:26:06

使用 XPath

/Document/Tests/Test

选择测试节点。然后在 foreach 中使用 XPathNavigator.SelectSingleNode

foreach (XPathNavigator val in it)
{
    testData = new TestData();
    testData.TestPosition = pos;
    testData.TestName = val.SelectSingleNode(nav.Compile("Name")).Value;
    testData.TestValue = val.SelectSingleNode(nav.Compile("Value")).Value;
    Datas.Add(testData);
    pos++;
}

或者使用此 XPath:

/Document/Tests/Test/*

它选择所有节点。

Use XPath

/Document/Tests/Test

It selects test nodes. Then in foreach use XPathNavigator.SelectSingleNode:

foreach (XPathNavigator val in it)
{
    testData = new TestData();
    testData.TestPosition = pos;
    testData.TestName = val.SelectSingleNode(nav.Compile("Name")).Value;
    testData.TestValue = val.SelectSingleNode(nav.Compile("Value")).Value;
    Datas.Add(testData);
    pos++;
}

Or use this XPath:

/Document/Tests/Test/*

It selects all nodes.

孤独患者 2024-12-22 16:26:06
 XPathNodeIterator it = nav.Select("/Document[Tests/Test/Name]");

这将返回所有包含测试/测试/名称的文档元素,

现在您可以确定,如果您扫描文档 - 您将拥有 3 个叶子。

 XPathNodeIterator it = nav.Select("/Document[Tests/Test/Name]");

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文