如何从C#中的xml读取特定节点? [英] How to read a particular node from xml in C#?
本文介绍了如何从C#中的xml读取特定节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下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>
我必须读取位于属性为 Name ="AAA"
的Segment节点下的每个Node的值,即Node1,Node2,Node3,Node4.我怎样才能做到这一点.我指的是来自stackoverflow的以下链接,但这对我不起作用.
I have to read value of each Node i.e, Node1, Node2, Node3, Node4 which are under Segment node whose attribute ie Name = "AAA"
. How can I do this. I am referring following link from stackoverflow but thats not working for me.
How to read attribute value from XmlNode in C#?
我需要这样的输出
让我有四个字符串变量 strNode1,strNode2,strNode3,strNode4
.我想将值存储在以下四个变量中
Lets I have four sting variables strNode1, strNode2, strNode3, strNode4
. I want to store values in above four variables like following
strNode1 = "hello"
strNode2 = "john"
strNode3 = "hi"
strNode4 = "marry"
推荐答案
我找到了解决问题的简单方法
I found a simple solution for my problem
XmlNodeList xnList = doc.SelectNodes("/Loop/Loop/Segment[@Name='AAA']");
foreach (XmlNode xn in xnList)
{
if (xn.HasChildNodes)
{
foreach (XmlNode item in xn.ChildNodes)
{
Console.WriteLine(item.InnerText);
}
}
}
这篇关于如何从C#中的xml读取特定节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文