使用c#在XML中选择特定节点 [英] Select specific node in XML using c#
本文介绍了使用c#在XML中选择特定节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我正在使用的默认XML代码。
< 姓名 >
< 文字 > 您好< /文字 >
< 文字 > Hello < /文字 >
< 文字 > 您如何< /文字 >
< /姓名 >
你能建议我如何选择第二个< text>节点。我的意思是我想打印Hello作为输出。
谢谢
venkat。
解决方案
你好Venkat,
正如你已经使用c#4.0,然后尝试使用System.Xml.Linq。这太棒了....
XDocument xmldoc = XDocument.Parse(< name>< text> Hi< text> Hello< text>你好吗? );
XElement elResult = xmldoc.Descendants(Text)。Single(x =>(string)x.Value ==Hello);
微笑
Prakash
这是:
XmlDocument xd = new XmlDocument();
xd.LoadXml( @ < name>
< text> Hi< / text>
< text> Hello< / text>
< text>你好< / text>
< / name>);
var result = xd.SelectNodes( /名称/文字跨度>);
if ((result!= null )& &(result.Count> = 2 ))
Console.WriteLine(结果[ 1 ] .InnerText);
Console.ReadKey();
希望有所帮助。
阅读本文,希望对您有所帮助了解XML
http://www.functionx。 com / csharp2 / xml / Lesson02d.htm [ ^ ]
Hi,
This is the default XML code i am having.
<Name>
<Text>Hi </Text>
<Text>Hello </Text>
<Text>How are you </Text>
</Name>
Could you please suggest me how to select second <text> node. I mean i want to print "Hello" as output.
Thanks
venkat.
解决方案
Hi Venkat,
As you already using c#4.0, then try this stuff with System.Xml.Linq. It's amazing....
XDocument xmldoc = XDocument.Parse("<name><text>Hi<text>Hello<text>How are you");
XElement elResult = xmldoc.Descendants("Text").Single(x => (string)x.Value == "Hello");
Smile
Prakash
Here it is :
XmlDocument xd= new XmlDocument(); xd.LoadXml(@"<name> <text>Hi </text> <text>Hello </text> <text>How are you </text> </name>"); var result = xd.SelectNodes("/Name/Text"); if ((result!=null) && (result.Count>=2)) Console.WriteLine(result[1].InnerText); Console.ReadKey();
Hope it helps.
Read this, I hope it helps you understand XML
http://www.functionx.com/csharp2/xml/Lesson02d.htm[^]
这篇关于使用c#在XML中选择特定节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文