如何解析在.net C#多单XML元素 [英] How to parse multiple single xml elements in .Net C#

查看:91
本文介绍了如何解析在.net C#多单XML元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想deserialise一些XML,它包含的.Net C#多个单独的XML元素,像这样:

I'm trying to deserialise some xml that contains multiple single xml elements in .Net C#, like so:

<Root>
 <Status>OK</Status>
 <Person>
  <Name>Element 1</Name>
 </Person>
 <Person>
  <Name>Element 2</Name>
 </Person>
</Root>

的个人节点不是在一个<人>< /人> ; ,所以我不能使用 [XmlArray] 属性

The Person nodes are not in a <Persons></Persons>, therefore I cannot use the [XmlArray] attribute.

有谁知道。要做到这一点,而不必使用与的XDocument中的XPath。

Does anyone know to do that, without having to use the XPath with XDocument.

感谢

推荐答案

如果使用.net 3.5或以上,使用LINQ到XML:

If using .Net 3.5 or above, use Linq-to-XML:

string xml = "<root>...</root>";
XDocument doc = XDocument.Parse(xml); // Use .Load() if loading from a file
String status = doc.Root.Element("status").Value;
IEnumerable<string> personNames = doc.Root.Descendants("person").Select(x => x.Element("name").Value);

这篇关于如何解析在.net C#多单XML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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