c# - Creating XML document with Two root nodes -
i want create xml 2 root nodes,
<?xml version="1.0" encoding="ibm437"?> <header1> <header2> <fracc>6004</fracc> <txncode>hth</txncode> <reason>testing</reason> <timeout>20</timeout> <rdate>2/3/2015 12:00:00 am</rdate> <rtime>6/18/2015 1:20:00 pm</rtime> <seqno>5</seqno> <prefix>8</prefix> <msgtype>trr</msgtype> <sendto>trr</sendto> <replyto>trr</replyto> </header2> </header1>
my code this, i'm unable add 2 root elements code, it's must use xmldocument
class.
xmldocument xmldoc = new xmldocument(); xmlnode rootnode = xmldoc.createelement("header" ); xmldoc.appendchild(rootnode); xmlnode accountnode = xmldoc.createelement("fracc"); accountnode.innertext = infracc; rootnode.appendchild(accountnode); xmlnode txnnode = xmldoc.createelement("txncode"); txnnode.innertext = intxncode; rootnode.appendchild(txnnode); xmlnode reasonnode = xmldoc.createelement("reason"); reasonnode.innertext = inreason; rootnode.appendchild(reasonnode); xmlnode timeoutnode = xmldoc.createelement("timeout"); timeoutnode.innertext = intimeout.tostring(); rootnode.appendchild(timeoutnode); xmlnode rdatenode = xmldoc.createelement("rdate"); rdatenode.innertext = indate.tostring(); rootnode.appendchild(rdatenode); xmlnode rtimenode = xmldoc.createelement("rtime"); rtimenode.innertext = intime.tostring(); rootnode.appendchild(rtimenode); xmlnode seqnonode = xmldoc.createelement("seqno"); seqnonode.innertext = inseqno.tostring(); rootnode.appendchild(seqnonode); xmlnode prefixnode = xmldoc.createelement("prefix"); prefixnode.innertext = inprefix.tostring(); rootnode.appendchild(prefixnode); xmlnode msgtypenode = xmldoc.createelement("msgtype"); msgtypenode.innertext = inmsgtype; rootnode.appendchild(msgtypenode); xmlnode sendtonode = xmldoc.createelement("sendto"); sendtonode.innertext = insendto; rootnode.appendchild(sendtonode); xmlnode replytonode = xmldoc.createelement("replyto"); replytonode.innertext = inreplyto; rootnode.appendchild(replytonode); xmldoc.save("boc.xml"); xmldoc.load("boc.xml"); xmldoc.save(console.out); return xmldoc;
and output
<?xml version="1.0" encoding="ibm437"?> <header> <fracc>6004</fracc> <txncode>ttt</txncode> <reason>testing</reason> <timeout>20</timeout> <rdate>2/3/2015 12:00:00 am</rdate> <rtime>6/18/2015 1:20:00 pm</rtime> <seqno>5</seqno> <prefix>8</prefix> <msgtype>tt</msgtype> <sendto>t</sendto> <replyto>t</replyto> </header>
please me add 2 root nodes.
you not adding 2 root elements.
change lines of code
xmldocument xmldoc = new xmldocument(); xmlnode rootnode = xmldoc.createelement("header" ); xmldoc.appendchild(rootnode);
like below -
xmldocument xmldoc = new xmldocument(); xmlnode rootnode1 = xmldoc.createelement("header1"); xmldoc.appendchild(rootnode1); xmlnode rootnode = xmldoc.createelement("header2"); rootnode1.appendchild(rootnode);
Comments
Post a Comment