Click here to Skip to main content
15,851,242 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello, I have an xml document
XML
<Person Attribute1="Name" Attribute2="LastName" Attribute3="Age" Attribute4="Price">
<Person1 Attribute1Person1="NamePerson" Attribute2Person1="LastNamePerson" />
<Person2 Attribute1Person2="NamePerson" Attribute2Person2="LastNamePerson" />
<Person3 Attribute1Person3="NamePerson" Attribute2Person3="LastNamePerson" />
<Person4 Attribute1Person4="NamePerson" Attribute2Person4="LastNamePerson" />
</Person>
 
<Man Attribute1="Name" Attribute2="LastName" Attribute3="Age" Attribute4="Price">
<Man1 Attribute1Person1="NameMan" Attribute2Person1="LastNameMan" />
<Man2 Attribute1Person2="NameMan" Attribute2Person2="LastNameMan" />
<Man3 Attribute1Person3="NameMan" Attribute2Person3="LastNameMan" />
<Man4 Attribute1Person4="NameMan" Attribute2Person4="LastNameMan" />
</Man>
 
<Women Attribute1="Name" Attribute2="LastName" Attribute3="Age" Attribute4="Price">
<Women1 Attribute1Person1="NameWomen" Attribute2Person1="LastNameWomen" />
<Women2 Attribute1Person2="NameWomen" Attribute2Person2="LastNameWomen" />
<Women3 Attribute1Person3="NameWomen" Attribute2Person3="LastNameWomen" />
<Women4 Attribute1Person4="NameWomen" Attribute2Person4="LastNameWomen" />
</Women>

I try to extract this document with linq to have only the parent of each element.

XML
<Person Attribute1="Name" Attribute2="LastName" Attribute3="Age" Attribute4="Price">
<Man Attribute1="Name" Attribute2="LastName" Attribute3="Age" Attribute4="Price">
<Women Attribute1="Name" Attribute2="LastName" Attribute3="Age" Attribute4="Price">


What I have tried:

i get the xml document as xdocument and i tired to get the descendant of parson,Man,Women but it give me his child.
C#
XDocument xdoc = XDocument.Load(this.XMLFile);
var xelementperson=xdoc.xml.Descendants("Person");
var xelementMan=xdoc.xml.Descendants("Man");
var xelementWomen=xdoc.xml.Descendants("Women");
Posted
Updated 24-Oct-20 23:11pm

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900