0

我正在制作一个包含子根节点的 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>

我哪里错了?

4

2 回答 2

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();


}

和输出:

<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>
于 2012-12-13T10:49:07.557 回答
1

您正在创建您的根元素“许可证”:

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

但是您没有将其附加到文档中。此外,您要多次附加到 doc :

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

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

像这样添加你的根元素:

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

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

而且我必须同意 LB:LINQ to XML 对于这样的东西要容易得多(但这不是问题;))

于 2012-12-13T10:48:57.710 回答