Merge 2 XMLs based on their attribute value using C# or XSLT
first xml is:
<study stdyname="jyia"> <subject sbjid="001"> <visit visitid="a"> <itemgroup oid="a1" name="gulzar" age="23" /> <itemgroup oid="a2" name="tej" age="32" /> </visit> <visit visitid="b"> <itemgroup oid="b1" name="ahmed" age="22" /> <itemgroup oid="b2" name="praks" age="42" /> </visit> </subject> <subject sbjid="002"> <visit visitid="c"> <itemgroup oid="c1" name="gul" age="13" /> <itemgroup oid="c2" name="te" age="12" /> </visit> </subject> </study>
second xml is:
<study stdyname="jyia"> <subject sbjid="001"> <visit visitid="b"> <itemgroup oid="bb1" name="ahme" age="2" /> <itemgroup oid="bb2" name="prak" age="2" /> </visit> </subject> <subject sbjid="003"> <visit visitid="d"> <itemgroup oid="d1" name="gull" age="113" /> <itemgroup oid="d2" name="tejj" age="112" /> </visit> </subject> </study>
output be:
<study stdyname="jyia"> <subject sbjid="001"> <visit visitid="a"> <itemgroup oid="a1" name="gulzar" age="23" /> <itemgroup oid="a2" name="tej" age="32" /> </visit> <visit visitid="b"> <itemgroup oid="b1" name="ahmed" age="22" /> <itemgroup oid="b2" name="praks" age="42" /> <itemgroup oid="bb1" name="ahme" age="2" /> <itemgroup oid="bb2" name="prak" age="2" /> </visit> </subject> <subject sbjid="002"> <visit visitid="c"> <itemgroup oid="c1" name="gul" age="13" /> <itemgroup oid="c2" name="te" age="12" /> </visit> </subject> <subject sbjid="003"> <visit visitid="d"> <itemgroup oid="d1" name="gull" age="113" /> <itemgroup oid="d2" name="tejj" age="112" /> </visit> </subject> </study>
i want merge above 2 xmls output shown in output code. please me out..i new c# code, if possible please provide whole code that. plesae guide me if there way round (using xslt)to that.here merging done attribute value.
thanks in advance, appreciated.
try this:
xmldocument doca = new xmldocument(); doca.load(@"c:\xml\data1.xml"); xmldocument docb = new xmldocument(); docb.load(@"c:\xml\data2.xml"); foreach (xmlnode subjectb in docb.documentelement.childnodes) { xmlnode subjecta = doca.selectsinglenode("//subject[@sbjid='" + subjectb.attributes["sbjid"].value + "']"); if (subjecta == null) { xmlnode importedsubject = doca.importnode(subjectb, true); doca.documentelement.appendchild(importedsubject); } else { xmlnode visitb = subjectb.firstchild; xmlnode visita = subjecta.selectsinglenode("visit[@visitid='" + visitb.attributes["visitid"].value + "']"); if (visita == null) { xmlnode importedvisit = doca.importnode(visitb, true); subjecta.appendchild(importedvisit); } else { foreach (xmlnode itemgroupb in visitb.childnodes) { xmlnode importeditemgroup = doca.importnode(itemgroupb, true); visita.appendchild(importeditemgroup); } } } } doca.save(@"c:\xml\merged.xml");
Visual Studio Languages , .NET Framework > Visual C#
Comments
Post a Comment