Insert node in xml document using c# -


i trying insert xml node in xml document @ specific position.

this xml:

<envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"> <body> <readcontract xmlns="http://implementation.company.schema.reference"> <contactnumbers>10158</contactnumbers> <productgroups>0085</productgroups> <indicationbalanceinfo>false</indicationbalanceinfo> <indicationblocked>true</indicationblocked> </readcontract> </body> </envelope>

and trying insert tag <productgroups>0093</productgroups> below tag <productgroups>0085</productgroups>

expecting below:

<envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"> <body> <readcontract xmlns="http://implementation.company.schema.reference"> <contactnumbers>10158</contactnumbers> <productgroups>0085</productgroups> <productgroups>0093</productgroups> <indicationbalanceinfo>false</indicationbalanceinfo> <indicationblocked>true</indicationblocked> </readcontract> </body> </envelope>

used below c# code achieve it.

     xmldocument doc = new xmldocument();             string inputxml = this.stservicecallactivity5.inputenvelope.innerxml.tostring(); //here inputxml contains whole xml document.             string addxml = "<productgroups>0093</productgroups>";             doc.loadxml(inputxml);             xmldocumentfragment xmldocfrag = doc.createdocumentfragment();             xmldocfrag.innerxml = addxml;             xmlelement parentele = doc.documentelement;             parentele.appendchild(xmldocfrag); 

and returns value <envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"> <body> <readcontract xmlns="http://implementation.company.schema.reference"> <contactnumbers>10158</contactnumbers> <productgroups>0085</productgroups> <productgroups>0093</productgroups> <indicationbalanceinfo>false</indicationbalanceinfo> <indicationblocked>true</indicationblocked> </readcontract> </body> <productgroups xmlns="">0093</productgroups> </envelope>

am newbie c# code, kindly me xml doc expected. appreciated.

thanks, madhan

looks namespaces causing problem. worked me:

xmldocument doc = new xmldocument(); doc.loadxml(file.readalltext("xmlfile1.xml")); xmlnamespacemanager ns = new xmlnamespacemanager(doc.nametable); ns.addnamespace("ns1", "http://schemas.xmlsoap.org/soap/envelope/"); ns.addnamespace("ns2", "http://implementation.company.schema.reference"); var rootnode = doc.selectsinglenode("//ns1:envelope", ns); var readcontractnode = rootnode.firstchild.firstchild; var newnode = doc.createnode(xmlnodetype.element, "productgroups", "http://implementation.company.schema.reference"); newnode.innertext = "0093"; readcontractnode.insertafter(newnode, readcontractnode.selectsinglenode("//ns2:productgroups", ns)); 

or if don't fancy namespaces me, can try bit more "brute-forcy" approach:

xmldocument doc = new xmldocument(); doc.loadxml(file.readalltext("xmlfile1.xml")); var newnode = doc.createnode(xmlnodetype.element, "productgroups", "http://implementation.company.schema.reference"); newnode.innertext = "0093"; doc.firstchild.firstchild.firstchild.insertafter(newnode, doc.firstchild.firstchild.firstchild.firstchild.nextsibling); 

it can optimized think helps make point root cause different namespaces in document.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -