如何遍历Xml Child节点并获得值 [英] How to iterate through Xml Child node and get there value

查看:120
本文介绍了如何遍历Xml Child节点并获得值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想迭代这个Xml文件并希望在数组中获取其子节点值C#wpf



Hi I want to iterate through this Xml file and want to fetch its children nodes value in an array C# wpf

<UDK>
  <Gesture_0>Y + U</Gesture_0>
  <Gesture_1>N + B + M</Gesture_1>
  <Gesture_2>H + J</Gesture_2>
  <Gesture_3>B + V + N</Gesture_3>
  <Gesture_4>E + T</Gesture_4>
  <Down>L</Down>
</UDK><pre></pre>





例如在

数组[0] = Y + U

数组[1] = N + B等等;



e.g. in
array[0]=Y+U
array[1] = N+B and so on;

推荐答案

LINQ to XML很容易实现。在 XElement 中解析xml [ ^ ]实例,并使用 Elements() [ ^ ]循环遍历子元素的方法。从那里你可以填充一个数组。
It's easy to do so with LINQ to XML. Parse the xml in an XElement[^] instance, and use the Elements()[^] method to loop through the child elements. From there you can fill an array.


看看这个:



http://stackoverflow.com/questions/6442024/getting-specified-node-values-from-xml-document [ ^ ]


Try the following code 
string xml = @"<UDK>
               <Gesture_0>Y + U</Gesture_0>
               <Gesture_1>N + B + M</Gesture_1>
               <Gesture_2>H + J</Gesture_2>
               <Gesture_3>B + V + N</Gesture_3>
               <Gesture_4>E + T</Gesture_4>
               <Down>L</Down>
               </UDK>";

XDocument document = XDocument.Parse(xml);

var result = document.XPathSelectElements(@"*/*").Select(e => e.Value).ToArray()


这篇关于如何遍历Xml Child节点并获得值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆