我创建了一个类Employee
,如下所示
class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public int Salary { get; set; }
public static Employee[] GetAllEmployee()
{
Employee[] Emp = new Employee[5];
Emp[0] = new Employee { Id = 101, Name = "Mery", Gender = "Female", Salary = 10000 };
Emp[1] = new Employee { Id = 102, Name = "Lucy", Gender = "Female", Salary = 12000 };
Emp[2] = new Employee { Id = 103, Name = "Jeny", Gender = "Female", Salary = 15000 };
Emp[3] = new Employee { Id = 104, Name = "Lilly", Gender = "Female",Salary = 10000 };
Emp[4] = new Employee { Id = 105, Name = "Sony", Gender = "Female", Salary = 17000 };
return Emp;
}
现在我想检索薪水超过10000的员工的姓名。 所以我使用这个代码,但它给出了错误,需要进行哪些修正?
IEnumerable<string> Names = from Emps in XDocument.Load(@"path\Data.xml")
.Descendants("Employee")
where (int)Emps.Element("Salary") > 10000
select Emps.Element("Name").Value;
foreach(string name in Names)
{
Console.WriteLine(name);
Console.ReadLine();
}
答案 0 :(得分:1)
以下代码有效。 xml文档在根级别不能有数组。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
Company company = new Company();
XmlSerializer serializer = new XmlSerializer(typeof(Company));
StreamWriter writer = new StreamWriter(FILENAME);
serializer.Serialize(writer, company);
writer.Flush();
writer.Close();
writer.Dispose();
IEnumerable<string> Names = from Emps in XDocument.Load(FILENAME)
.Descendants("Employee")
where (int)Emps.Element("Salary") > 10000
select Emps.Element("Name").Value;
foreach (string name in Names)
{
Console.WriteLine(name);
}
Console.ReadLine();
}
}
public class Company
{
[XmlElement("Employee")]
public Employee[] employees = Employee.GetAllEmployee();
}
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public int Salary { get; set; }
public static Employee[] GetAllEmployee()
{
Employee[] Emp = new Employee[5];
Emp[0] = new Employee { Id = 101, Name = "Mery", Gender = "Female", Salary = 10000 };
Emp[1] = new Employee { Id = 102, Name = "Lucy", Gender = "Female", Salary = 12000 };
Emp[2] = new Employee { Id = 103, Name = "Jeny", Gender = "Female", Salary = 15000 };
Emp[3] = new Employee { Id = 104, Name = "Lilly", Gender = "Female", Salary = 10000 };
Emp[4] = new Employee { Id = 105, Name = "Sony", Gender = "Female", Salary = 17000 };
return Emp;
}
}
}