如何将对象集合与复杂对象属性绑定?
我正在尝试加载由 Employee 对象集合组成的 XML 数据。以下函数适用于简单数据类型(如 String 和 Int)的属性。我想知道如何导入复杂类型的数据类型。例如,
这个函数工作正常:
private void LoadData()
{
XDocument employeesDoc = XDocument.Load("Employees.xml");
List<Employee> data = (from employee in employeesDoc.Descendants("Employee")
select new Employee
{
FirstName= employee.Attribute("FirstName").Value,
LastName = employee.Attribute("LastName ").Value,
PhoneNumber = employee.Attribute("PhoneNumber").Value
}).ToList();
Employees.ItemsSource = data;
}
这是 Employee 类:
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string PhoneNumber { get; set; }
public Department Department { get; set; }
}
这是 Department 类:
public class Department
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Employee Manager { get; set; }
}
那么,如果我的 XML 文件看起来像这样:
<Employees>
<Employee FirstName="John" LastName="Summers" PhoneNumber="703-548-7841" Department="Finance"></Employee>
<Employee FirstName="Susan" LastName="Hughey" PhoneNumber="549-461-7962" Department="HR"></Employee>
那么,如果 Department 是一个复杂的对象并且它是 XML 文件中的一个字符串,那么如何才能我更改 LoadData() 函数以将其导入到我的 Employee 对象集合中?
I am trying to load XML data that consists of a collection of Employee objects. The following function works fine for properties that are simple data types like String and Int. I am wondering how can I import data types of complex type. For example,
This function works fine:
private void LoadData()
{
XDocument employeesDoc = XDocument.Load("Employees.xml");
List<Employee> data = (from employee in employeesDoc.Descendants("Employee")
select new Employee
{
FirstName= employee.Attribute("FirstName").Value,
LastName = employee.Attribute("LastName ").Value,
PhoneNumber = employee.Attribute("PhoneNumber").Value
}).ToList();
Employees.ItemsSource = data;
}
Here is the Employee class:
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string PhoneNumber { get; set; }
public Department Department { get; set; }
}
Here is the Department class:
public class Department
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Employee Manager { get; set; }
}
So, if my XML file looks like so:
<Employees>
<Employee FirstName="John" LastName="Summers" PhoneNumber="703-548-7841" Department="Finance"></Employee>
<Employee FirstName="Susan" LastName="Hughey" PhoneNumber="549-461-7962" Department="HR"></Employee>
So, if the Department is a complex object and it is a string in XML file, how can I change my LoadData() function to import it into my Employee object collection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您有几个选择,但一切都取决于您获取 XML 的方式(它是如何保存的)。
(在我看来)最简单的阅读方法是:
更多示例:
http://msdn.microsoft.com/en-us/library/he66c7f1。 aspx
另一方面,如果您(出于某种原因)应该使用 LINQ - 请看这里:
LINQ to XML:创建复杂匿名类型
在这里
http://blogs.msdn.com/b/xmlteam/archive/2007/03/24/streaming-with-linq-to-xml-part-2.aspx
希望有帮助!
You have a couple of options, but everything depends how you get your XML (how it was saved).
The easiest way (to my mind) to read is:
More Examples here:
http://msdn.microsoft.com/en-us/library/he66c7f1.aspx
From the other hand if you (for some reasone) should use LINQ - take a look here:
LINQ to XML: creating complex anonymous type
and here
http://blogs.msdn.com/b/xmlteam/archive/2007/03/24/streaming-with-linq-to-xml-part-2.aspx
Hope it helps!
如果您可以在加载员工之前获取所有部门的列表,则可以将部门放入
Dictionary
中,其中键是部门名称。然后,您可以为每个员工加载正确的部门:代码可能需要修改,具体取决于您在该部门不存在或某人没有指定任何部门时要执行的操作。这段代码在这两种情况下都会抛出异常。
If you can get the list of all departments before you load the employees, you could put the departments into a
Dictionary
, where the key is the name of the department. You could then load the correct department for each employee:The code might need modification, depending on what you want to do if the department doesn't exist or if someone doesn't have any department specified. This code would throw an exception in both cases.