c# - How to compare two big XML files item by item efficiently? -


i plan implement method compare 2 big xml files (but less 10,000 element lines each of other).

the method below works, doesn't when file more 100 lines. begin slowly. how can find more efficient solution. maybe need high c# programming design or better algorithm in c# & xml handling.

thanks comments in advance.

//remove item not in event xml , confaddition xml files xmldocument doc = new xmldocument(); doc.load(xmlfile_alarmsettingup);  bool isnewalid_event = false; bool isnewalid_confaddition = false; int alid = 0;  xmlnodelist xnlist = doc.selectnodes("/equipment/alarmsettingup/enabledalids/alid");  foreach (xmlnode xn in xnlist) {                             xmlattributecollection attcol = xn.attributes;      (int = 0; < attcol.count; ++i)     {         if (attcol[i].name == "alid")         {             alid = int.parse(attcol[i].value.tostring());             break;         }     }      //alid = int.parse(attcol[1].value.tostring());      xmldocument docevent_alarm = new xmldocument();     docevent_alarm.load(xmlfile_event);     xmlnodelist xnlistevent_alarm = docevent_alarm.selectnodes("/equipment/alarms/alid");     foreach (xmlnode xnevent_alarm in xnlistevent_alarm)     {         xmlattributecollection attcolevent_alarm = xnevent_alarm.attributes;         int alidevent_alarm = int.parse(attcolevent_alarm[1].value.tostring());         if (alid == alidevent_alarm)         {             isnewalid_event = false;             break;         }         else         {             isnewalid_event = true;             //break;         }     }      xmldocument docconfaddition_alarm = new xmldocument();     docconfaddition_alarm.load(xmlfile_confaddition);     xmlnodelist xnlistconfaddition_alarm = docconfaddition_alarm.selectnodes("/equipment/alarms/alid");     foreach (xmlnode xnconfaddition_alarm in xnlistconfaddition_alarm)     {         xmlattributecollection attcolconfaddition_alarm = xnconfaddition_alarm.attributes;         int alidconfaddition_alarm = int.parse(attcolconfaddition_alarm[1].value.tostring());         if (alid == alidconfaddition_alarm)         {             isnewalid_confaddition = false;             break;         }         else         {             isnewalid_confaddition = true;             //break;         }     }                              if ( isnewalid_event && isnewalid_confaddition )     {         // store root node of destination document xmlnode         xmlnode rootdest = doc.selectsinglenode("/equipment/alarmsettingup/enabledalids");         rootdest.removechild(xn);     }  } doc.save(xmlfile_alarmsettingup); 

my xml file this. 2 xml files same style. except time 1 of them may modified app. that's why need compare them if modified.

<?xml version="1.0" encoding="utf-8"?> <equipment xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">   <licence licenseid="" licensepath="" />   <!--alarm setting xml file-->   <alarmsettingup>     <enabledalids>       <alid logicalname="misc_ev_rm_station_already_reserved" alid="536870915" alcd="7" altx="misc_station  1   unitname  2   slotid reserved" ceon="misc_alarmon_ev_rm_station_already_reserved" ceoff="misc_alarmoff_ev_rm_station_already_reserved" />       <alid logicalname="misc_ev_rm_seq_read_error" alid="536870916" alcd="7" altx="misc_sequence id  1 d step  2 d read error wafer in  3   unitname  4   slotid" ceon="misc_alarmon_ev_rm_seq_read_error" ceoff="misc_alarmoff_ev_rm_seq_read_error" /> ... ... ...     </enabledalids>   </alarmsettingup> </equipment> 

the "alid/@alid" seems key, first thing (before foreach (xmlnode xn in xnlist)) build dictionary (assuming unique) on docevent_alarm.selectnodes("/equipment/alarms/alid") @alid values - can of work without o(n*m) performance - it'll more o(n+m) (which big difference).

var lookup = new dictionary<string, xmlelement>(); foreach(xmlelement el in docevent_alarm.selectnodes("/equipment/alarms/alid")) {     lookup.add(el.getattribute("alid"), el); } 

then can use:

xmlelement other; if(lookup.trygetvalue(otherkey, out other)) {    // exists; element in "other" } else {    // doesn't exist } 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -