我有以下XML:
<Loop Name="MasterData">
<Loop Name="SlaveData">
<Segment Name="AAA">
<Node1>hello</Node1>
<Node2>john</Node2>
<Node3>hi</Node3>
<Node4>marry</Node4>
</Segment>
<Segment Name="BBB">
<Node1>00</Node1>
<Node2> </Node2>
<Node3>00</Node3>
<Node4> </Node4>
</Segment>
</Loop>
</Loop>我必须读取每个节点的值,即Node1、Node2、Node3、Node4,它们位于其属性为Name = "AAA"的分段节点下。我该怎么做呢。我指的是来自stackoverflow的以下链接,但这对我不起作用。
How to read attribute value from XmlNode in C#?
我需要这样的输出
假设我有四个刺变量strNode1, strNode2, strNode3, strNode4。我希望将值存储在上述四个变量中,如下所示
strNode1 = "hello"
strNode2 = "john"
strNode3 = "hi"
strNode4 = "marry"发布于 2013-04-22 20:36:22
您可以使用XmlDocument和XPath:
XmlDocument doc = new XmlDocument();
doc.Load(xml);
foreach(XmlNode node in doc.SelectNodes("//Segment[@Name='AAA']/node()"))
{
string name = node.Name;
string value = node.innerText;
// ...
}https://stackoverflow.com/questions/16147142
复制相似问题