2010-10-08 51 views
0

我有一個C#程序循環瀏覽表單庫中的所有表單(啓用瀏覽器),並將XML節點注入每個表單(對於新提升的字段) 。出於某種原因,當XML保存回表單時,前幾個標籤被剝離。具體而言,這些標籤是:InfoPath表單XML的程序化更新剝離標籤

<?xml version="1.0"?> 
<?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:Contractor-DB-Form:-myXSD-2009-09-10T18-19-55" solutionVersion="1.0.1.1100" productVersion="12.0.0.0" PIVersion="1.0.0.0" href="http://echouat.rbs.us/npe/FormServerTemplates/Contractor_DB_Form.xsn"?> 
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.2"?> 
<?mso-infoPath-file-attachment-present?>" 

我的代碼來更新XML如下:

private static SPListItem InsertXmlNode(SPListItem infoPathForm, string nodeToUpdateStr, string nodeToInsertStr, 
     string nodeInnerXmlStr, string firstNode) 
    { 
     //load form into xml document 
     byte[] fileBytes = infoPathForm.File.OpenBinary(); 
     MemoryStream itemStream = new MemoryStream(fileBytes); 
     //Stream itemStream = infoPathForm.File.OpenBinary(); 
     XmlDocument xmlDoc = new XmlDocument(); 
     XmlNamespaceManager xmlNameSpaceMgr = new XmlNamespaceManager(xmlDoc.NameTable); 
     xmlNameSpaceMgr.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-10T18:19:55"); 

     xmlDoc.Load(itemStream); 
     itemStream.Close(); 

     //inject xml 
     XmlNode nodeToUpdate = xmlDoc.SelectSingleNode(firstNode + nodeToUpdateStr, xmlNameSpaceMgr); 

     //only insert if doesn't already exist 
     if (xmlDoc.SelectSingleNode(firstNode + nodeToUpdateStr + "/" + nodeToInsertStr, xmlNameSpaceMgr) == null) 
     { 
      updateCounter++; 

      XmlNode nodeToInsert = xmlDoc.CreateNode(XmlNodeType.Element, nodeToInsertStr, "http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-10T18:19:55"); 
      nodeToInsert.InnerText = nodeInnerXmlStr; 
      nodeToUpdate.AppendChild(nodeToInsert); 

      //get binary data for updated xml 
      byte[] newXmlData = Encoding.UTF8.GetBytes(xmlDoc.DocumentElement.OuterXml); 
      MemoryStream newMemStream = new MemoryStream(newXmlData); 

      //write updated binary data to the form 
      infoPathForm.File.SaveBinary(newMemStream); 

      newMemStream.Close(); 

      infoPathForm.File.Update(); 
     } 

     return infoPathForm; 
    } 

添加新節點的正常工作;我可以看到新的XML格式正確。只是將文件從MemoryStream加載到XmlDocument對象後,標籤纔會被剝離。一旦這些標籤丟失,這些表單將不再在IP中打開。

請幫助!

謝謝!

回答

0

更改其內容的行:

byte[] newXmlData = Encoding.UTF8.GetBytes(xmlDoc.DocumentElement.OuterXml); 

閱讀:

byte[] newXmlData = Encoding.UTF8.GetBytes(xmlDoc.OuterXml);