How to remove duplicate xml nodes with attribute using LINQ? -



How to remove duplicate xml nodes with attribute using LINQ? -

i have next xml structure:

<movie> <profile> </profile> <address> </address> <details detail1="1" detail2="1"> <moviestart>09:20:00</moviestart> <movietime date="2015-01-20" hour="07:05:00" /> <code>ba</code> <moviearrive code="mah" place="maharashtra" /> <moviedepart code="jam" place="jammu" /> <type>std</type> </details> <details detail1="2" detail2="2"> <moviestart>08:00:00</moviestart> <movietime date="2015-01-25" hour="07:35:00" /> <code>bi</code> <moviearrive code="bih" place="bihar" /> <moviedepart code="mys" place="mysore" /> <type>std</type> </details> <details detail1="1" detail2="1"> <moviestart>09:20:00</moviestart> <movietime date="2015-01-20" hour="07:05:00" /> <code>ba</code> <moviearrive code="mah" place="maharashtra" /> <moviedepart code="jam" place="jammu" /> <type>std</type> </details> <details detail1="2" detail2="2"> <moviestart>08:00:00</moviestart> <movietime date="2015-01-25" hour="07:35:00" /> <code>bi</code> <moviearrive code="bih" place="bihar" /> <moviedepart code="mys" place="mysore" /> <type>std</type> </details> </movie>

wanted eliminate lastly 2 sets of details section after comparing each , every value of element , attributes. tried below code no luck.

var elements = (from el in doc.descendants("movie").descendants("details") select el).groupby(x => x.value).select(x => x.first());

above code works element value , ignores attribute value. how can remove duplicates after comparing?

after duplicate removal xml should below:

<movie> <profile> </profile> <address> </address> <details detail1="1" detail2="1"> <moviestart>09:20:00</moviestart> <movietime date="2015-01-20" hour="07:05:00" /> <code>ba</code> <moviearrive code="mah" place="maharashtra" /> <moviedepart code="jam" place="jammu" /> <type>std</type> </details> <details detail1="2" detail2="2"> <moviestart>08:00:00</moviestart> <movietime date="2015-01-25" hour="07:35:00" /> <code>bi</code> <moviearrive code="bih" place="bihar" /> <moviedepart code="mys" place="mysore" /> <type>std</type> </details> </movie>

one more clarification, if xml modified below :

<movie> <profile> </profile> <address> </address> <details detail1="1" detail2="1"> <moviestart>09:20:00</moviestart> <movietime date="2015-01-20" hour="07:05:00" /> <code>ba</code> <moviearrive code="mah" place="maharashtra" /> <moviedepart code="jam" place="jammu" /> <type>std</type> </details> <details detail1="2" detail2="1"> <moviestart>08:00:00</moviestart> <movietime date="2015-01-25" hour="07:35:00" /> <code>bi</code> <moviearrive code="bih" place="bihar" /> <moviedepart code="mys" place="mysore" /> <type>std</type> </details> <details detail1="1" detail2="2"> <moviestart>09:20:00</moviestart> <movietime date="2015-01-20" hour="07:05:00" /> <code>ba</code> <moviearrive code="mah" place="maharashtra" /> <moviedepart code="jam" place="jammu" /> <type>std</type> </details> <details detail1="2" detail2="2"> <moviestart>08:00:00</moviestart> <movietime date="2015-01-25" hour="07:35:00" /> <code>bi</code> <moviearrive code="bih" place="bihar" /> <moviedepart code="mys" place="mysore" /> <type>std</type> </details> </movie>

only attributes of details having different values.. suggestions pls?

you can utilize xnode.deepequals() check if 2 nodes have same markup, illustration :

var details = doc.descendants("details").tolist(); foreach (xelement detail in details) { //get node other current detail having same markup detail var duplicate = doc.descendants("details") .firstordefault(o => o != detail && xnode.deepequals(o, detail)); //if exist, current detail duplicate -> remove if(duplicate != null) detail.remove(); } console.writeline(doc.tostring());

output :

<movie> <profile></profile> <address></address> <details detail1="1" detail2="1"> <moviestart>09:20:00</moviestart> <movietime date="2015-01-20" hour="07:05:00" /> <code>ba</code> <moviearrive code="mah" place="maharashtra" /> <moviedepart code="jam" place="jammu" /> <type>std</type> </details> <details detail1="2" detail2="2"> <moviestart>08:00:00</moviestart> <movietime date="2015-01-25" hour="07:35:00" /> <code>bi</code> <moviearrive code="bih" place="bihar" /> <moviedepart code="mys" place="mysore" /> <type>std</type> </details> </movie>

another approach based on linq query have tried, print same result :

var elements = (from el in doc.descendants("movie").descendants("details") select el).groupby(x => x.tostring()) .where(x => x.count() > 1) .select(x => x.first()); foreach (xelement element in elements) { element.remove(); } console.writeline(doc.tostring());

xml

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -