从Xml中获取所需数据

时间:2013-06-15 12:56:18

标签: xml linq

我有一个XML数据示例如下

<?xml version="1.0" encoding="utf-8" ?>
<Employees>
 <Employee>
    <EmpId>1</EmpId>
    <Name>Shawn</Name>   
 </Employee>
 <Employee>
    <EmpId>2</EmpId>
    <Name>Neil</Name>
 </Employee>
 <Employee>
    <EmpId>3</EmpId>
    <Name>Kate</Name>
  </Employee>
 <Employee>
    <EmpId>4</EmpId>
    <Name>Robert</Name>
</Employee>
</Employees>

如何选择第二和第三个员工数据?

2 个答案:

答案 0 :(得分:2)

使用Skip()Take()扩展程序。

实施例

XElement doc=XElement.Load("yourdata.xml");
var list = doc.Elements("Employee").Skip(1).Take(2);

答案 1 :(得分:0)

使用Enumerable.TakeEnumerable.Skip

XDocument document = XDocument.Parse(yourXmlString);
IEnumerable<XElement> results = document.Root.Elements().Skip(1).Take(2);