-1

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.

ekad
  • 14,436
  • 26
  • 44
  • 46
  • 5
    What is the reason for you to have two roots in xml? It is kind a violation of xml standarts, such an xml will not be well-formed, and most of xml parsers (or even any parser) will fail to parse it. – Andrey Korneyev Jun 19 '15 at 11:16
  • 3
    And by the way - xml you've posted doesn't actually has two roots. Instead it has opening ``, then `` inside of it, and after closing of `` you're closing `` - which was never opened. So what do you actually trying to achieve? – Andrey Korneyev Jun 19 '15 at 11:21
  • 2
    I can't actually see from your code how you have attempted to add a second header. Or do you mean you are getting an error when you try adding the second one in. Do you mean that you want to add a root note and then a parent node which will have children? You are able to do that but without knowing the context it may be difficult to help you out. – Keithin8a Jun 19 '15 at 11:21
  • @AndyKorneyev snap ;) – Keithin8a Jun 19 '15 at 11:22
  • yeah im adding second root element like this XmlNode rootNode2 = xmlDoc.CreateElement("header2" ); xmlDoc.AppendChild(rootNode); – user3073993 Jun 19 '15 at 11:23

2 Answers2

0

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);
Amit
  • 882
  • 6
  • 13
0

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

ekad
  • 14,436
  • 26
  • 44
  • 46