Abirami Rajendran
asked on
Query XML using Linq
How do I get the employee list from below XML
<?xml version="1.0" encoding="utf-8"?>
<Employee>
<Some_XML_1 />
<Some_XML_2 />
<EmployeeList>
<id>1</id>
<name>abc</name>
</EmployeeList>
<EmployeeList>
<id>2</id>
<name>def</name>
</EmployeeList>
<EmployeeList>
<id>3</id>
<name>hij</name>
</EmployeeList>
</Employee>
I need a collection of ID & Name. Is it possible in LINQ or should we go back to DOM
<?xml version="1.0" encoding="utf-8"?>
<Employee>
<Some_XML_1 />
<Some_XML_2 />
<EmployeeList>
<id>1</id>
<name>abc</name>
</EmployeeList>
<EmployeeList>
<id>2</id>
<name>def</name>
</EmployeeList>
<EmployeeList>
<id>3</id>
<name>hij</name>
</EmployeeList>
</Employee>
I need a collection of ID & Name. Is it possible in LINQ or should we go back to DOM
My version - use LinqPad for easy development:
XElement data = XElement.Parse(
@"<Employee>
<Some_XML_1 />
<Some_XML_2 />
<EmployeeList>
<id>1</id>
<name>abc</name>
</EmployeeList>
<EmployeeList>
<id>2</id>
<name>def</name>
</EmployeeList>
<EmployeeList>
<id>3</id>
<name>hij</name>
</EmployeeList>
</Employee>");
var employees = data.Descendants("Employee List");
foreach (var employee in employees)
{
var id = employee.Element("id").Val ue;
var name = employee.Element("name").V alue;
// Rest of code...
}
Gary Davis
XElement data = XElement.Parse(
@"<Employee>
<Some_XML_1 />
<Some_XML_2 />
<EmployeeList>
<id>1</id>
<name>abc</name>
</EmployeeList>
<EmployeeList>
<id>2</id>
<name>def</name>
</EmployeeList>
<EmployeeList>
<id>3</id>
<name>hij</name>
</EmployeeList>
</Employee>");
var employees = data.Descendants("Employee
foreach (var employee in employees)
{
var id = employee.Element("id").Val
var name = employee.Element("name").V
// Rest of code...
}
Gary Davis
ASKER
Thanks, But I get the following error for the the below code:
XElement xmlElem = XElement.Parse(strXML);
var list = xmlElem.Descendants("Emplo yeeList")
.Select(e => new
{
ID = e.Descendants("id").First( ).Value,
Name = e.Descendants("name").Firs t().Value
})
.ToList();
'System.Collections.Generi c.IEnumera ble<System .Xml.Linq. XElement>' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'System.Collections.Generi c.IEnumera ble<System .Xml.Linq. XElement>' could be found (are you missing a using directive or an assembly reference?)
XElement xmlElem = XElement.Parse(strXML);
var list = xmlElem.Descendants("Emplo
.Select(e => new
{
ID = e.Descendants("id").First(
Name = e.Descendants("name").Firs
})
.ToList();
'System.Collections.Generi
using System.Linq;
using System.Xml.Linq;
Also add reference to System.Xml and System.Xml.Linq from add references in VS
using System.Xml.Linq;
Also add reference to System.Xml and System.Xml.Linq from add references in VS
ASKER
Sorry both the solutions doesnt work. I get the count as 0 for the list & its not even going inside the foreach loop. But there are records in the XML
Could it be because of xml namespace in the EmployeeList node?
Could it be because of xml namespace in the EmployeeList node?
There is no namespace shown in your data example above but if there is one in your actual data, it could have an effect.
ASKER
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
A little better performance within the loop:
XNamespace ns = "http://a.com";
XName Id = ns + "id";
XName Name = ns + "name";
var employees = data.Descendants(ns + "EmployeeList");
employees.Dump("Employees" );
foreach (var employee in employees)
{
var id = employee.Element(Id).Value ;
var name = employee.Element(Name).Val ue;
// Rest of code...
}
Details at http://msdn.microsoft.com/en-us/library/cc716786.aspx
BuggyCoder's solution will also work with the appropriate edits.
Gary
XNamespace ns = "http://a.com";
XName Id = ns + "id";
XName Name = ns + "name";
var employees = data.Descendants(ns + "EmployeeList");
employees.Dump("Employees"
foreach (var employee in employees)
{
var id = employee.Element(Id).Value
var name = employee.Element(Name).Val
// Rest of code...
}
Details at http://msdn.microsoft.com/en-us/library/cc716786.aspx
BuggyCoder's solution will also work with the appropriate edits.
Gary
Open in new window