代码生成的 XML 输出中的 C# XML 格式错误

本文关键字:XML 格式 错误 输出 代码生成 | 更新日期: 2023-09-27 18:35:30

所以我有一个已经存在的文件,我读取了这个文件并向其中添加了一些节点。 由于所需处理的性质,我的代码处于循环中,因此对于给定数据表中的每一行,我打开现有文件,写入新节点,然后关闭文件。

第一次迭代完美地插入节点组。 此后的每次迭代都是有问题的,并且以某种方式添加到第一个分组而不是创建自己的分组。

这是每个分组应该的样子,这就是它生成第一个分组的方式:

  <item identifier="ITEM-F2D7FEDF240B4DCCBF346DBE2C47AC89" identifierref="RES-770DCE40C5BA4E97B1E3B3DB49BBBD4F" isvisible="true" parameters="">
    <title>Title1</title>
    <adlcp:datafromlms xmlns:adlcp="http://www.imsproject.org/xsd/imscp_rootv1p1p2">
    </adlcp:datafromlms>
  </item>

但是,一旦整个事情被处理,它最终看起来像这样:

  <item identifier="ITEM-F2D7FEDF240B4DCCBF346DBE2C47AC89" identifierref="RES-770DCE40C5BA4E97B1E3B3DB49BBBD4F" isvisible="true" parameters="">
    <title>Title1</title>
    <adlcp:datafromlms xmlns:adlcp="http://www.imsproject.org/xsd/imscp_rootv1p1p2">
    </adlcp:datafromlms>
    <title>Title2</title>
    <adlcp:datafromlms xmlns:adlcp="http://www.imsproject.org/xsd/imscp_rootv1p1p2">
    </adlcp:datafromlms>
    <title>Title3</title>
    <adlcp:datafromlms xmlns:adlcp="http://www.imsproject.org/xsd/imscp_rootv1p1p2">
    </adlcp:datafromlms>
    <title>Title4</title>
    <adlcp:datafromlms xmlns:adlcp="http://www.imsproject.org/xsd/imscp_rootv1p1p2">
    </adlcp:datafromlms>
    <title>Title5</title>
    <adlcp:datafromlms xmlns:adlcp="http://www.imsproject.org/xsd/imscp_rootv1p1p2">
    </adlcp:datafromlms>
  </item>
  <item identifier="ITEM-4D80AFFE59D04E2188F39908B9325961" identifierref="RES-A9CFDC9208714DAF9EA351D4656A7EBC" isvisible="true" parameters="" />
  <item identifier="ITEM-F4EDB38AD0D74CC38722E6D1A8D67E24" identifierref="RES-E2F92D4C5165482386421944053EE933" isvisible="true" parameters="" />
  <item identifier="ITEM-BF1C7474919B4B22BC300F98034ABDD1" identifierref="RES-8A0ED1C94CA44A71A07A8A4A5DA2A528" isvisible="true" parameters="" />
  <item identifier="ITEM-156731B2ABB14AB29135CBF5D8CBCFF3" identifierref="RES-D452D539C49A4D65BC3A8AC6B16DE718" isvisible="true" parameters="" />

所以基本上它最终将大部分新数据绑定到现有节点(我不想要它的地方),并在组织组的底部创建新的项目节点(这是它应该在的地方)。 我需要将标题和 adlcp 条目附加到每个新项目节点下。

这是我正在使用的代码。 请记住,此代码对同一文件执行多次,每组条目执行一次。 代码创建了一个额外的节点,它进入另一个称为资源的地方,但该部分工作正常,所以我没有将其包含在上面的 XML 摘录中。

            string strItem = Guid.NewGuid().ToString("N").ToUpper(); // GUID for random unique value.
            string strRes = Guid.NewGuid().ToString("N").ToUpper(); // GUID for random unique value.
            XmlDocument docXMLFile = new XmlDocument();
            docXMLFile.Load(resultPath + "imsmanifest.xml"); // Load file
            #region Item Element Creation
            XmlNode xItem = docXMLFile.CreateNode(XmlNodeType.Element, "item", docXMLFile.DocumentElement.NamespaceURI);
            XmlAttribute xIdentifier = docXMLFile.CreateAttribute("identifier");
            XmlAttribute xIdentifierRef = docXMLFile.CreateAttribute("identifierref");
            XmlAttribute xIsVisible = docXMLFile.CreateAttribute("isvisible");
            XmlAttribute xParameters = docXMLFile.CreateAttribute("parameters");
            xIdentifier.Value = "ITEM-" + strItem;
            xIdentifierRef.Value = "RES-" + strRes;
            xIsVisible.Value = "true";
            xParameters.Value = "";
            xItem.Attributes.Append(xIdentifier);
            xItem.Attributes.Append(xIdentifierRef);
            xItem.Attributes.Append(xIsVisible);
            xItem.Attributes.Append(xParameters);
            // NOTE - the docXMLFile.DocumentElement.NamespaceURI GETS RID OF XMLNS="" WHICH IS BULLSHIT.
            XmlNode xTitle = docXMLFile.CreateNode(XmlNodeType.Element, "title", docXMLFile.DocumentElement.NamespaceURI);
            if ((dataRow["product_name"].ToString() + " - " + dataRow["topic_name"].ToString()).Count() > 255)
                xTitle.InnerText = (dataRow["product_name"].ToString() + " - " + dataRow["topic_name"].ToString()).Substring(0, 255);
            else
                xTitle.InnerText = dataRow["product_name"].ToString() + " - " + dataRow["topic_name"].ToString();
            XmlNode xADLCPDataFromLMS = docXMLFile.CreateNode(XmlNodeType.Element, "adlcp:datafromlms", docXMLFile.DocumentElement.NamespaceURI);
            xADLCPDataFromLMS.InnerText = dataRow["datafromlms"].ToString();
            // This is where the new stuff gets inserted.
            docXMLFile.GetElementsByTagName("organization")[0].InsertAfter(xItem, docXMLFile.GetElementsByTagName("organization")[0].LastChild);
            docXMLFile.GetElementsByTagName("item")[0].InsertAfter(xTitle, docXMLFile.GetElementsByTagName("item")[0].LastChild);
            docXMLFile.GetElementsByTagName("item")[0].InsertAfter(xADLCPDataFromLMS, docXMLFile.GetElementsByTagName("item")[0].LastChild);
            #endregion
            #region Resource Element Creation
            XmlNode xResource = docXMLFile.CreateNode(XmlNodeType.Element, "resource", docXMLFile.DocumentElement.NamespaceURI);
            XmlAttribute xRefIdentifier = docXMLFile.CreateAttribute("identifier");
            XmlAttribute xRefADLCP = docXMLFile.CreateAttribute("adlcp:scormtype");
            XmlAttribute xRefHREF = docXMLFile.CreateAttribute("href");
            XmlAttribute xRefType = docXMLFile.CreateAttribute("type");
            xRefIdentifier.Value = "RES-" + strRes;
            xRefADLCP.Value = "sco";
            xRefHREF.Value = dataRow["launch_url"].ToString().ToLower();
            xRefType.Value = "webcontent";
            xResource.Attributes.Append(xRefIdentifier);
            xResource.Attributes.Append(xRefADLCP);
            xResource.Attributes.Append(xRefHREF);
            xResource.Attributes.Append(xRefType);
            docXMLFile.GetElementsByTagName("resources")[0].InsertAfter(xResource, docXMLFile.GetElementsByTagName("resources")[0].LastChild);
            #endregion
            docXMLFile.Save(resultPath + "imsmanifest.xml"); //save

代码生成的 XML 输出中的 C# XML 格式错误

嗯,这就是问题所在:

docXMLFile.GetElementsByTagName("item")[0]
          .InsertAfter(xTitle,
                       docXMLFile.GetElementsByTagName("item")[0].LastChild);
docXMLFile.GetElementsByTagName("item")[0]
          .InsertAfter(xADLCPDataFromLMS, 
                       docXMLFile.GetElementsByTagName("item")[0].LastChild);

您显式使用第一个item元素。我怀疑你真的只是想要:

xItem.AppendChild(xTitle);
xItem.AppendChild(xADLCPDataFromLMS);

毕竟,您执行的操作是将元素附加到新创建的item元素中,对吗?