Creating XML document with Two root nodes

1.4k views Asked by At

I want to create XML with two root nodes, like this

<?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 is like this, I'm unable to add two root elements with my code, it's a must to 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 my output is this

<?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 help me to add two root nodes.

2

There are 2 answers

0
Amit On

You are not adding 2 root elements.

Change your 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);
0
ekad On

Based on your sample output, it's clear that <header1> is the root element and <header2> is inside <header1>, so all you need to do is append <header2> inside <header1> and append the rest of the elements inside <header2>. This code should work

XmlDocument xmlDoc = new XmlDocument();
XmlNode rootNode = xmlDoc.CreateElement("header1");
xmlDoc.AppendChild(rootNode);

XmlNode rootNode2 = xmlDoc.CreateElement("header2");
rootNode.AppendChild(rootNode2);

XmlNode accountNode = xmlDoc.CreateElement("fracc");
accountNode.InnerText = Infracc;
rootNode2.AppendChild(accountNode);

XmlNode txnNode = xmlDoc.CreateElement("txncode");
txnNode.InnerText = Intxncode;
rootNode2.AppendChild(txnNode);

XmlNode reasonNode = xmlDoc.CreateElement("reason");
reasonNode.InnerText = Inreason;
rootNode2.AppendChild(reasonNode);

XmlNode timeoutNode = xmlDoc.CreateElement("timeout");
timeoutNode.InnerText = Intimeout.ToString();
rootNode2.AppendChild(timeoutNode);

XmlNode rdateNode = xmlDoc.CreateElement("rdate");
rdateNode.InnerText = Indate.ToString();
rootNode2.AppendChild(rdateNode);

XmlNode rtimeNode = xmlDoc.CreateElement("rtime");
rtimeNode.InnerText = Intime.ToString();
rootNode2.AppendChild(rtimeNode);

XmlNode seqnoNode = xmlDoc.CreateElement("seqno");
seqnoNode.InnerText = Inseqno.ToString();
rootNode2.AppendChild(seqnoNode);

XmlNode prefixNode = xmlDoc.CreateElement("prefix");
prefixNode.InnerText = Inprefix.ToString();
rootNode2.AppendChild(prefixNode);

XmlNode msgtypeNode = xmlDoc.CreateElement("msgtype");
msgtypeNode.InnerText = Inmsgtype;
rootNode2.AppendChild(msgtypeNode);

XmlNode sendtoNode = xmlDoc.CreateElement("sendto");
sendtoNode.InnerText = Insendto;
rootNode2.AppendChild(sendtoNode);

XmlNode replytoNode = xmlDoc.CreateElement("replyto");
replytoNode.InnerText = Inreplyto;
rootNode2.AppendChild(replytoNode);

xmlDoc.Save("boc.xml");
xmlDoc.Load("boc.xml");
xmlDoc.Save(Console.Out);           

return xmlDoc;

Working fiddle: https://dotnetfiddle.net/EevsJq