Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get attribute value using 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();
}

Using the code above, I can get the result below: enter image description here

But I need the column WorkPhone's value 424-555-0545 instead of Home and the column HomePhone's value 423-555-0124 instead of Home.

What should I do for that?

like image 493
CHANDRA Avatar asked Jan 26 '13 18:01

CHANDRA


1 Answers

Use the Where method:

For the Home phone number:

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

For the Work phone number:

emp.Elements("Phone").Single(phoneElement => phoneElement.Attribute("Type").Value == "Work").Value
  • emp.Elements("Phone") is a enumerable on all "Phone" elements of emp.
  • Single will get the element that satisfy the specified property (if there are 0 or more than 1 element that satisfy the property, an error is raised).
  • phoneElement.Attribute("Type").Value is the value of the attribute "Type" (ie "Home" or "Work")

Then, your code should be:

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,
                };

If the element emp may have no Work phone or Home phone number, the above code will raise an exception in the Single. To deal with this case you have to change your code to:

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

SingleOrDefault will equal null if no "Phone" element satisfy the condition and the string cast on a XElement is equivalent to XElement.Value.

like image 108
Cédric Bignon Avatar answered Oct 09 '22 10:10

Cédric Bignon