InfoPath表单XML的编程更新正在剥离标签

时间:2010-10-08 14:20:43

标签: infopath infopath-2007

我有一个C#程序循环遍历表单库中的所有表单(已启用浏览器),并为每个表单注入一个XML节点(对于新提升的字段)。出于某种原因,当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中打开。

请帮助!

谢谢!

1 个答案:

答案 0 :(得分:0)

更改以下行:

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

阅读:

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