如何访问具有相同父节点的不同 XML 节点?
我有一个像这样的 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 读取数据,如下所示(ztr
是 Test
的实例>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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用
SelectSingleNode
,而是使用SelectNodes
相反,我认为您当前的 xpath 应该足够了。您必须重写GetTestData
方法来处理XmlNodeList
的迭代,以解析出正确的float
值。Rather than using
SelectSingleNode
, useSelectNodes
instead, your current xpath should suffice I think. You'll have to re-write yourGetTestData
method to handle iterating through theXmlNodeList
to parse out the correctfloat
value.修改您的
GetTestData()
方法(及其相应的调用者):Modify your
GetTestData()
method (and its caller accordingly):