如何解析.Net C#中的多个单个xml元素

问题描述

|| 我正在尝试反序列化一些包含.Net C#中多个xml元素的xml,如下所示:
<Root>
 <Status>OK</Status>
 <Person>
  <Name>Element 1</Name>
 </Person>
 <Person>
  <Name>Element 2</Name>
 </Person>
</Root>
Person节点不在
<Persons></Persons>
中,因此我不能使用
[XmlArray]
属性。 有谁知道这样做,而不必将XPath与XDocument一起使用。 谢谢     

解决方法

        如果使用.Net 3.5或更高版本,请使用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);