阅读背景:

如何使用linq到xml获取属性值?

来源:互联网 
<Employees>
  <Employee>
    <EmpId>1</EmpId>
    <Name>Sam</Name>
    <Sex>Male</Sex>
    <Phone Type="Home">423-555-0124</Phone>
    <Phone Type="Work">424-555-0545</Phone>
  </Employee>
</Employees>

private void Window_Loaded(object sender, RoutedEventArgs e)
    {

        emplyeeDetails = XDocument.Load(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + "\LinqToXml\Xmls\" + "Employees.xml");
        var emplyees = from emp in emplyeeDetails.Descendants("Employee").Take(10)
                       orderby emp.Element("EmpId").Value ascending
                       select new
                       {
                           Id = emp.Element("EmpId").Value,
                           Name = emp.Element("Name").Value,
                           Sex = emp.Element("Sex").Value,
                           WorkPhone=emp.Element("Phone").Attribute("Type").Value,
                           HomePhone = emp.Element("Phone").Attribute("Type").Value,                               

                       };
        DgrdEmployeeDetails.ItemsSource = emplyees.ToList();
    }
<Employees>
  <Employee>
    <EmpId>1</EmpId>
 



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: