2012-05-25 63 views
1

所以我有一个已经存在的文件,并且我读取了这个文件并向它添加了一些节点。由于所需处理的性质,我将代码放在一个循环中,因此对于给定数据表中的每一行,我打开现有文件,写入新节点并关闭文件。C#XML代码生成的XML输出中的格式错误

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

这是每个分组应该是什么样子,这是怎么产生的第一个:

<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 

回答

3

嗯,这就是问题所在:

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元素,对不对?

+0

这正是我一直在寻找的。我不知道为什么我没有这样做! – user1366062