c# - Remove Duplicate XML Records -
i wanted remove duplicate records in xml far unable unsure of how can go doing this, here xml , can see there 4 duplicate records. want remove itemgrp node due having same rateclass element in itemgrp node
<?xml version="1.0" encoding="utf-8" ?> <fare_1 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"> <fare_1_details xmlns="http://xml.someplace"> <itemgrp> <itemnb> <itemnumberdetails> <number>01</number> </itemnumberdetails> </itemnb> <farequalifitem> <additionalfaredetails> <rateclass>dt21p</rateclass> </additionalfaredetails> <discountdetails> <farequalifier>725</farequalifier> </discountdetails> </farequalifitem> </itemgrp> <itemgrp> <itemnb> <itemnumberdetails> <number>02</number> </itemnumberdetails> </itemnb> <farequalifitem> <additionalfaredetails> <rateclass>dt21p</rateclass> </additionalfaredetails> <discountdetails> <farequalifier>725</farequalifier> </discountdetails> </farequalifitem> </itemgrp> <itemgrp> <itemnb> <itemnumberdetails> <number>03</number> </itemnumberdetails> </itemnb> <farequalifitem> <additionalfaredetails> <rateclass>cv76j</rateclass> </additionalfaredetails> <discountdetails> <farequalifier>352</farequalifier> </discountdetails> </farequalifitem> </itemgrp> </fare_1_details> <fare_1_details xmlns="http://xml.someplace"> <itemgrp> <itemnb> <itemnumberdetails> <number>01</number> </itemnumberdetails> </itemnb> <farequalifitem> <additionalfaredetails> <rateclass>th31l</rateclass> </additionalfaredetails> <discountdetails> <farequalifier>722</farequalifier> </discountdetails> </farequalifitem> </itemgrp> <itemgrp> <itemnb> <itemnumberdetails> <number>02</number> </itemnumberdetails> </itemnb> <farequalifitem> <additionalfaredetails> <rateclass>gk51t</rateclass> </additionalfaredetails> <discountdetails> <farequalifier>726</farequalifier> </discountdetails> </farequalifitem> </itemgrp> <itemgrp> <itemnb> <itemnumberdetails> <number>03</number> </itemnumberdetails> </itemnb> <farequalifitem> <additionalfaredetails> <rateclass>gk51t</rateclass> </additionalfaredetails> <discountdetails> <farequalifier>726</farequalifier> </discountdetails> </farequalifitem> </itemgrp> </fare_1_details> </fare_1>
what tried far xmldocument:
nametable nt = new nametable(); xmlnamespacemanager m = new xmlnamespacemanager(nt); m.addnamespace("xsi", "http://www.w3.org/2001/xmlschema-instance"); m.addnamespace("a", "http://xml.someplace"); xmlnodelist nodes = xmltree.selectnodes("//fare_1/fare_1_details/itemgrp", m); xmlnode actionnode = xmltree.selectsinglenode("//fare_1"); (int = 1; < nodes.count; i++) { actionnode.removechild(nodes[i]); }
i have been able come solution writing code:
string file = file.readalltext(appdomain.currentdomain.basedirectory + "xml.txt"); var xmldoc = new xmldocument(); xmldoc.loadxml(file); var el = xmldoc.selectsinglenode("/fare_1"); var c = el.childnodes; list<string> rateclassvalues = new list<string>(); foreach (xmlnode x in c) { foreach(xmlnode ch in x.childnodes) { var rateclassvalue = ch.firstchild.nextsibling.firstchild.firstchild.innertext; if (!rateclassvalues.contains(rateclassvalue)) { // add value list keep track of rate class values rateclassvalues.add(rateclassvalue); } else if (rateclassvalues.contains(rateclassvalue)) { // delete duplicate itemgrp parent xmlnode parent = ch.parentnode; parent.removechild(ch); } } } xmldoc.save(appdomain.currentdomain.basedirectory + "xmlnew.txt");
this code removes itemgrp has duplicate rateclass innertext. running code gives result:
<?xml version="1.0" encoding="utf-8"?> <fare_1 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"> <fare_1_details xmlns="http://xml.someplace"> <itemgrp> <itemnb> <itemnumberdetails> <number>01</number> </itemnumberdetails> </itemnb> <farequalifitem> <additionalfaredetails> <rateclass>dt21p</rateclass> </additionalfaredetails> <discountdetails> <farequalifier>725</farequalifier> </discountdetails> </farequalifitem> </itemgrp> <itemgrp> <itemnb> <itemnumberdetails> <number>03</number> </itemnumberdetails> </itemnb> <farequalifitem> <additionalfaredetails> <rateclass>cv76j</rateclass> </additionalfaredetails> <discountdetails> <farequalifier>352</farequalifier> </discountdetails> </farequalifitem> </itemgrp> </fare_1_details> <fare_1_details xmlns="http://xml.someplace"> <itemgrp> <itemnb> <itemnumberdetails> <number>01</number> </itemnumberdetails> </itemnb> <farequalifitem> <additionalfaredetails> <rateclass>th31l</rateclass> </additionalfaredetails> <discountdetails> <farequalifier>722</farequalifier> </discountdetails> </farequalifitem> </itemgrp> <itemgrp> <itemnb> <itemnumberdetails> <number>02</number> </itemnumberdetails> </itemnb> <farequalifitem> <additionalfaredetails> <rateclass>gk51t</rateclass> </additionalfaredetails> <discountdetails> <farequalifier>726</farequalifier> </discountdetails> </farequalifitem> </itemgrp> </fare_1_details> </fare_1>
i hope able hit nail on head. cheers!
Comments
Post a Comment