C# 使用两个根节点创建XML文档

C# 使用两个根节点创建XML文档,c#,xml,xmldocument,xml-documentation,C#,Xml,Xmldocument,Xml Documentation,我想用两个根节点创建XML,如下所示 <?xml version="1.0" encoding="IBM437"?> <header1> <header2> <fracc>6004</fracc> <txncode>HTH</txncode> <reason>testing</reason> <timeout>20</timeout> <r

我想用两个根节点创建XML,如下所示

<?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>
我的输出是

<?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>

6004
ttt
测试
20
2015年2月3日12:00:00上午
2015年6月18日下午1:20:00
5.
8.
tt
T
T

请帮助我添加两个根节点。

您没有添加两个根元素

更改代码行

          XmlDocument xmlDoc = new XmlDocument();
          XmlNode rootNode = xmlDoc.CreateElement("header" );
          xmlDoc.AppendChild(rootNode);
如下-

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

根据您的示例输出,很明显,
是根元素,
内,因此您只需在
内追加
,并在
内追加其余元素。这个代码应该有效

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:

在xml中有两个根的原因是什么?这有点违反了xml标准,这样的xml格式不好,大多数xml解析器(甚至任何解析器)都无法解析它。顺便说一下,您发布的xml实际上没有两个根。相反,它有一个打开的
,然后
在它里面,关闭
之后,你就关闭了
——它从未打开过。那么,您实际上想要实现什么呢?我无法从您的代码中看到您是如何尝试添加第二个头的。或者,您的意思是,当您尝试将第二个添加到中时,会出现错误。您的意思是要添加根注释,然后添加将有子节点的父节点吗?您可以这样做,但在不了解上下文的情况下,可能很难帮助您。@AndyKorneyev snap;)是的,我正在添加第二个根元素,比如xmlnoderootnode2=xmlDoc.CreateElement(“header2”);AppendChild(rootNode);
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;