I need to merge XMLnodes elements based on the value of attribute(id) and generate the result to a list using C#
This is the original XML file:
<profiles>
<profile id="1" >
<name>John</name>
<age>23</age>
<sex>male</sex>
</profile >
<profile id="2" >
<name>Mark</name>
<age>60</age>
</profile >
<profile id="2" >
<sex>male</sex>
</profile >
</profiles>
and I need to work on it as it was:
<profiles>
<profile id="1" >
<name>John</name>
<age>23</age>
<sex>male</sex>
</profile >
<profile id="2" >
<name>Mark</name>
<age>60</age>
<sex>male</sex>
</profile >
</profiles>
here is my trial but it returns no thing
var employee = from emp in fileDoc.Descendants("profile")
group emp by (string) emp.Attribute("id")
into emps
select new Data
{
ID =emps.Last().Attribute("id") != null ? emps.Last().Attribute("id").Value: "",
ProfileName =emps.Elements("name") != null? emps.Elements("name").Last().Value: "",
Sex=emps.Elements("sex") != null? emps.Elements("sex").Last().Value: ""
};
1 Answer