2010-10-08 46 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);