在XML中添加Subroot元素

时间:2012-12-13 10:36:41

标签: c# asp.net xml xmldocument

我正在制作一个包含子根节点的XML文档。我正在使用XmlDocument并添加子节点。

这是我的代码:

  XmlDocument doc = new XmlDocument();
  XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);
  doc.AppendChild(dec);

  XmlElement root = doc.CreateElement("LICENSE");

  if (strGInfo == "")
  {
    strGInfo = "N/A";
  }

  XmlElement ginfo = doc.CreateElement("GENERAL_INFO");
  ginfo.InnerText = strGInfo;
  root.AppendChild(ginfo);


  if (strLNo == "")
  {
     strLNo = "N/A";
  }

  XmlElement subroot = doc.CreateElement("LICENSE_INFO");
  //XmlElement root1 = doc.CreateElement("LICENSE_INFO");

  XmlElement lno = doc.CreateElement("LICENCE_NO");
  lno.InnerText = txtLNo.Text;
  subroot.AppendChild(lno);

  if (strUID == "")
  {
     strUID = "N/A";
  }

  XmlElement uid = doc.CreateElement("USER_ID");
  uid.InnerText = txtUID.Text;
  subroot.AppendChild(uid);

  if (strOrg == "")
  {
    strOrg = "N/A";
  }

  XmlElement org = doc.CreateElement("ORGANIZATION");
  org.InnerText = txtOrg.Text;
  subroot.AppendChild(org);

            if (strUName == "")
            {
                strUName = "N/A";
            }

            XmlElement uname = doc.CreateElement("USER_NAME");
            uname.InnerText = txtUName.Text;
            subroot.AppendChild(uname);

            if (strSType == "")
            {
                strSType = "N/A";
            }

            XmlElement stype = doc.CreateElement("SOLUTION_TYPE");
            stype.InnerText = txtSType.Text;
            subroot.AppendChild(stype);

            if (strVer == "")
            {
                strVer = "N/A";
            }

            XmlElement ver = doc.CreateElement("VERSION");
            ver.InnerText = txtVer.Text;
            subroot.AppendChild(ver);

            XmlElement ltype = doc.CreateElement("LICENCE_TYPE");
            ltype.InnerText = drpLType.SelectedItem.Text;
            subroot.AppendChild(ltype);

            if (strMeapSupp == "")
            {
                strMeapSupp = "N/A";
            }

            XmlElement meapsupp = doc.CreateElement("MEAP_SUPPORT");
            meapsupp.InnerText = rdoMeapSupport.Text;
            subroot.AppendChild(meapsupp);

            XmlElement LicFrom = doc.CreateElement("LICENCE_FROM");
            LicFrom.InnerText = lblLFrom.Text;
            subroot.AppendChild(LicFrom);

            XmlElement LicTo = doc.CreateElement("LICENCE_TO");
            LicTo.InnerText = lblLTo.Text;
            subroot.AppendChild(LicTo);

            XmlElement suppfrom = doc.CreateElement("SUPPORT_FROM");
            suppfrom.InnerText = lblSuppFrom.Text;
            subroot.AppendChild(suppfrom);

            XmlElement suppto = doc.CreateElement("SUPPORT_TO");
            suppto.InnerText = lblSuppTo.Text;
            subroot.AppendChild(suppto);

            doc.AppendChild(subroot);

            XmlElement subroot2 = doc.CreateElement("LICENCE_CONSTRAINT");

            if (strMaxUsr == "")
            {
                strMaxUsr = "N/A";
            }

            XmlElement maxusr = doc.CreateElement("MAX_USER");
            maxusr.InnerText = txtMaxUsr.Text;
            subroot2.AppendChild(maxusr);

            if (strMaxMach == "")
            {
                strMaxMach = "N/A";
            }

            XmlElement maxmach = doc.CreateElement("MAX_MACHINE");
            maxmach.InnerText = txtMaxMach.Text;
            subroot2.AppendChild(maxmach);

            if (strMachIP == "")
            {
                strMachIP = "N/A";
            }

            doc.AppendChild(subroot2);

            XmlElement subroot3 = doc.CreateElement("MACHINE_INFO");

            XmlElement machip = doc.CreateElement("MACHINE_IP");
            machip.InnerText = txtMachIP.Text;
            subroot3.AppendChild(machip);

            if (strMachMac == "")
            {
                strMachMac = "N/A";
            }

            XmlElement machmac = doc.CreateElement("MACHINE_MAC");
            machmac.InnerText = txtMachMac.Text;
            subroot3.AppendChild(machmac);

            doc.AppendChild(subroot3);

            XmlElement subroot4 = doc.CreateElement("LICENCE_SIGNATURE");

            XmlElement UqID = doc.CreateElement("UNIQUE_ID");
            UqID.InnerText = txtUqID.Text;
            subroot4.AppendChild(UqID);

            doc.AppendChild(subroot4);

            doc.Save(@"D:\New.xml");

我的XML文档看起来像这样:

-<LICENSE>
  <GENERAL_INFO> </GENERAL INFO>
-<LICENSE_INFO>
   <LICENSE_NO> </LICENSE_NO>
   <USER_ID> </USER_ID> //etc
-<LICENCE_CONSTRAINT>
   <MAX_USER> </MAX_USER> 
   <MAX_MACHINE> </MAX_MACHINE> 
  </LICENCE_CONSTRAINT>
-<MACHINE_INFO>
  <MACHINE_IP> </MACHINE_IP> 
  <MACHINE_MAC> </MACHINE_MAC> 
 </MACHINE_INFO>
</LICENSE_INFO>

我哪里错了?

2 个答案:

答案 0 :(得分:3)

我会将Linq2Xml用于此

string xml = FormXml(licenceNo: "1",machineIP:"1.2.3.4",generalInfo:"some Info");

public string FormXml(
    string generalInfo="N/A",
    string licenceNo="N/A",
    string userID="N/A",
    string maxUser="N/A",
    string maxMachine="N/A",
    string machineIP="N/A",
    string machineMAC="N/A")
{
    return new XElement("LICENSE",
                new XElement("GENERAL_INFO", generalInfo),
                new XElement("LICENSE_INFO",
                        new XElement("LICENSE_NO", licenceNo),
                        new XElement("USER_ID", userID)),
                new XElement("LICENCE_CONSTRAINT",
                        new XElement("MAX_USER", maxUser),
                        new XElement("MAX_MACHINE", maxMachine)),
                new XElement("MACHINE_INFO",
                        new XElement("MACHINE_IP", machineIP),
                        new XElement("MACHINE_MAC", machineMAC))).ToString();


}

和OUTPUT:

<LICENSE>
  <GENERAL_INFO>some Info</GENERAL_INFO>
  <LICENSE_INFO>
    <LICENSE_NO>1</LICENSE_NO>
    <USER_ID>N/A</USER_ID>
  </LICENSE_INFO>
  <LICENCE_CONSTRAINT>
    <MAX_USER>N/A</MAX_USER>
    <MAX_MACHINE>N/A</MAX_MACHINE>
  </LICENCE_CONSTRAINT>
  <MACHINE_INFO>
    <MACHINE_IP>1.2.3.4</MACHINE_IP>
    <MACHINE_MAC>N/A</MACHINE_MAC>
  </MACHINE_INFO>
</LICENSE>

答案 1 :(得分:1)

您正在创建根元素&#34; LICENSE&#34;:

XmlElement root = doc.CreateElement("LICENSE");

但您没有将其附加到doc。 此外,您将多次附加到doc:

doc.AppendChild(subroot);
...
doc.AppendChild(subroot2);
...
doc.AppendChild(subroot3);

这是不可能的,因为XML文档只能有一个根。

像这样添加你的根元素:

XmlElement root = doc.CreateElement("LICENSE");
doc.AppendChild(root);

并将每个doc.AppendChild()更改为root.AppendChild()

我必须同意L.B。:LINQ to XML对于这样的东西来说要容易得多(但这不是问题;)