如何使用LINQ to XML获取属性值?

时间:2013-01-26 18:04:39

标签: c# xml linq linq-to-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();
}

使用上面的代码,我可以得到以下结果: enter image description here

但我需要列WorkPhone的值424-555-0545而不是Home而列HomePhone的值423-555-0124而不是Home }。

我该怎么做?

2 个答案:

答案 0 :(得分:13)

使用Where方法:

Home 电话号码:

emp.Elements("Phone").Single(phoneElement => phoneElement.Attribute("Type").Value == "Home").Value

工作电话号码:

emp.Elements("Phone").Single(phoneElement => phoneElement.Attribute("Type").Value == "Work").Value
  • emp.Elements("Phone")在所有&#34;电话&#34;上都是可枚举的。 emp的元素。
  • Single将获取满足指定属性的元素(如果有0个或多个元素满足该属性,则会引发错误。)
  • phoneElement.Attribute("Type").Value是属性的值&#34; Type&#34; (即&#34; Home&#34;或&#34; Work&#34;)

然后,您的代码应为:

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.Elements("Phone").Single(phoneElement => phoneElement.Attribute("Type").Value == "Home").Value,
                    HomePhone = emp.Elements("Phone").Single(phoneElement => phoneElement.Attribute("Type").Value == "Work").Value,
                };

如果元素emp可能没有工作电话或主页电话号码,则上述代码将在Single中引发异常。要处理这种情况,您必须将代码更改为:

(string)emp.Elements("Phone").SingleOrDefault(phoneElement => phoneElement.Attribute("Type").Value == "Home")
如果没有&#34;电话&#34; <{SingleOrDefault将等于null element满足条件,string上的XElement广告等同于XElement.Value

答案 1 :(得分:2)

即使员工存在Phone个元素,此代码仍然有效:

var emplyees = 
    from emp in emplyeeDetails.Descendants("Employee").Take(10)
    let phones = emp.Descendants("Phone")
    orderby (int)emp.Element("EmpId")
    select new
    {
        Id = (int)emp.Element("EmpId"),
        Name = (string)emp.Element("Name"),
        Sex = (string)emp.Element("Sex"),
        WorkPhone = (string)phones.FirstOrDefault(p => (string)p.Attribute("Type") == "Work"),
        HomePhone = (string)phones.FirstOrDefault(p => (string)p.Attribute("Type") == "Home")                               
    };

将投射元素用于stringint等,而不是访问Value属性。为什么?因为如果你的xml中有一些缺少的元素或属性,那么你将获得NullReferenceException。但是转换将返回默认值。因此,上面的代码将解析甚至xml,如下所示:

<Employees>
  <Employee>
    <EmpId>1</EmpId>
    <Name>Sam</Name>
    <Phone Type="Home">423-555-0124</Phone>
    <Phone>524-777-1234</Phone>
  </Employee>
</Employees>