i am trying select some elements from xml file and try to put the child nodes of an elements ina row,and need to put all selected elements in a list.i am new to asp this is my code but i cant get the values.....
public static DirectoryResult GetXmlEmployeeDetails(string searchParams, string orderBy, string direction, int limitFrom, int limitTo)
{
var empXmlPath = HostingEnvironment.ApplicationPhysicalPath + "DirectoryXml/Employee.xml";
var root = XElement.Load(empXmlPath);
if(searchParams!=string.Empty)
{
IEnumerable<XElement> totalResult = from el in root.Elements("EmployeeDetail")
where ((string)el.Element("EPFNO")).Contains(searchParams)
|| ((string)el.Element("EMPNAME")).Contains(searchParams)
|| ((string)el.Element("DESIGNATION")).Contains(searchParams)
|| ((string)el.Element("BR_NAME")).Contains(searchParams)
|| ((string)el.Element("DEPARTMENT")).Contains(searchParams)
select el;
Second ,If you want to iterate the child elements meet your satiation,you can try the code below:
XElement ex = XElement.Load(Server.MapPath("XMLFile1.xml"));
IEnumerable<XElement> q = from ele in ex.Elements("EmployeeDetail")
where ele.Element("EPFNO").Value.Contains(searchParams)||
ele.Element(“EMPNAME”).Value.Contains(searchParams)||
ele.Element(“DESIGNATION”).Value.Contains(searchParams)||
ele.Element(“BR_NAME”).Value.Contains(searchParams)||
ele.Element(“DEPARTMENT”).Value.Contains(searchParams)
select ele;
List<string> list=new List<string>();
foreach (XElement x in q.Elements()) {
list.add(x.Value);
}
return list;
More details about LINQ TO XML,please refer to link below:
None
0 Points
1 Post
how to select xml child nods and put it into list
May 17, 2014 07:04 AM|niroj90|LINK
i am trying select some elements from xml file and try to put the child nodes of an elements ina row,and need to put all selected elements in a list.i am new to asp this is my code but i cant get the values.....
public static DirectoryResult GetXmlEmployeeDetails(string searchParams, string orderBy, string direction, int limitFrom, int limitTo)
{
var empXmlPath = HostingEnvironment.ApplicationPhysicalPath + "DirectoryXml/Employee.xml";
var root = XElement.Load(empXmlPath);
if(searchParams!=string.Empty)
{
IEnumerable<XElement> totalResult = from el in root.Elements("EmployeeDetail")
where ((string)el.Element("EPFNO")).Contains(searchParams)
|| ((string)el.Element("EMPNAME")).Contains(searchParams)
|| ((string)el.Element("DESIGNATION")).Contains(searchParams)
|| ((string)el.Element("BR_NAME")).Contains(searchParams)
|| ((string)el.Element("DEPARTMENT")).Contains(searchParams)
select el;
}
return new DirectoryResult();
}
what's wrong with this one?
c asp.net linq xml
All-Star
16806 Points
2777 Posts
Re: how to select xml child nods and put it into list
May 18, 2014 11:39 PM|Kevin Shen - MSFT|LINK
Hi niroj90,
Based on your description ,I think you want to select one of the child elements of one parent element which contains searchParams,
and iterate the element ,store into the list.
Firsrt ,if you want to get the value of an element ,you should try code like below:
Second ,If you want to iterate the child elements meet your satiation,you can try the code below:
More details about LINQ TO XML,please refer to link below:
http://msdn.microsoft.com/en-us/bb387044
Hope it can help you.
Best Regards,
Kevin Shen.
c asp.net linq xml