如何访问具有相同父节点的不同 XML 节点?

发布于 2024-12-14 16:12:57 字数 2270 浏览 2 评论 0原文

我有一个像这样的 XML 文件:

<Document>
   <Tests>
      <Test>
          <Name>A</Name>
          <Value>1</Value>
      </Test>
      <Test>
          <Name>A</Name>
          <Value>2</Value>
      </Test>
      <Test>
          <Name>B</Name>
          <Value>10</Value>
      </Test>
      <Test>
          <Name>B</Name>
          <Value>20</Value>
      </Test>
   </Tests>
</Document>

我创建了一个类来保存每个 Test 节点的数据:

public class TestData
{
public string TestName {get; set;}
public float TestValue {get; set;}
}

并且我使用 XPATH 读取数据,如下所示(ztrTest 的实例>XmlDocument 而且我已经知道我要读取的 Test 的名称,因为它已填充到 ListView 中):

    public TestData GetTestData(string testName)
    {
        TestData data = new TestData();           

        data.TestName = testName;
        data.TestValue = float.Parse(ztr.SelectSingleNode("/Document/Tests/Test[Name = '" + testName + "')]/Value").InnerText, CultureInfo.InvariantCulture);

        return data;
    }

现在,问题是如果我想制作一个数据列表List 每个 TestData 仅引用 XML 文件中第一次出现的 Test 节点。我的意思是,假设我有一个 ListView,它已由 xml 文件中所有 Test 节点的名称填充。在本例中是这样的:

ListView = A,A,B,B 然后我将所有名称复制到 string[] 数组中

我使用此代码一次性读取它们:

private List<TestData> GetAllData()
{
    List<TestData> Datas = new List<TestData>();
    TestData data;

    foreach(string test in stringOfNames)
    {
       data = new TestData;
       data = GetTestData(test);
       Datas.add(data);
    }

    return Datas
}

正如我告诉你的此代码不会对不同的Test进行任何区分。它只返回 Test 的第一次出现,因此 GetAllData() 的结果将是:

Name     Value
A        1
A        1
B        10
B        10

但是我希望它像

Name     Value
A        1
A        2
B        10
B        20

你可以吗告诉我我做错了什么?以及如何解决这个问题?

PS 我在 stackoverflow 中编写了所有代码...它们没有编译,只是想象它们工作正常:D

I have a XML file like this:

<Document>
   <Tests>
      <Test>
          <Name>A</Name>
          <Value>1</Value>
      </Test>
      <Test>
          <Name>A</Name>
          <Value>2</Value>
      </Test>
      <Test>
          <Name>B</Name>
          <Value>10</Value>
      </Test>
      <Test>
          <Name>B</Name>
          <Value>20</Value>
      </Test>
   </Tests>
</Document>

I made a class to hold data for each Test node:

public class TestData
{
public string TestName {get; set;}
public float TestValue {get; set;}
}

And I read the data using XPATH like this (ztr is instance of XmlDocument And I already know the name of Test I am going to read, since it was populted into a ListView):

    public TestData GetTestData(string testName)
    {
        TestData data = new TestData();           

        data.TestName = testName;
        data.TestValue = float.Parse(ztr.SelectSingleNode("/Document/Tests/Test[Name = '" + testName + "')]/Value").InnerText, CultureInfo.InvariantCulture);

        return data;
    }

Now, te problem is that if I want to make a list of data as List<TestData> each TestData only refers to the first occurance of the Test node in the XML file. What I mean is lets imagin I have a ListView that is already populated by name of all Test nodes in my xml file. that is something like this in this case:

ListView = A,A,B,B and then I copy all names to a string[] array

And I use this code to read them all in one go:

private List<TestData> GetAllData()
{
    List<TestData> Datas = new List<TestData>();
    TestData data;

    foreach(string test in stringOfNames)
    {
       data = new TestData;
       data = GetTestData(test);
       Datas.add(data);
    }

    return Datas
}

As I told you This code does not make any distinguish between different Test. It just returns the first occurance of a Test so the result for GetAllData() will be:

Name     Value
A        1
A        1
B        10
B        10

BUT I want it to be like

Name     Value
A        1
A        2
B        10
B        20

Can you please tell me what I am doing wrong? and how to solve this problem?

P.S. I wrote all the codes just here in stackoverflow...they are not compiled, just imagine they work fine :D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

美男兮 2024-12-21 16:12:59

不要使用 SelectSingleNode,而是使用 SelectNodes 相反,我认为您当前的 xpath 应该足够了。您必须重写 GetTestData 方法来处理 XmlNodeList 的迭代,以解析出正确的 float 值。

Rather than using SelectSingleNode, use SelectNodes instead, your current xpath should suffice I think. You'll have to re-write your GetTestData method to handle iterating through the XmlNodeList to parse out the correct float value.

神爱温柔 2024-12-21 16:12:58

修改您的 GetTestData() 方法(及其相应的调用者):

    public TestData GetTestData(string testName, int pos) 
    { 
        TestData data = new TestData();            

        data.TestName = testName; 
        data.TestValue 
         = float.Parse(
ztr.SelectSingleNode("/Document/Tests/Test[Name = '" + testName + "')][" + pos + "]/Value").InnerText, CultureInfo.InvariantCulture); 

        return data; 
    }

Modify your GetTestData() method (and its caller accordingly):

    public TestData GetTestData(string testName, int pos) 
    { 
        TestData data = new TestData();            

        data.TestName = testName; 
        data.TestValue 
         = float.Parse(
ztr.SelectSingleNode("/Document/Tests/Test[Name = '" + testName + "')][" + pos + "]/Value").InnerText, CultureInfo.InvariantCulture); 

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