开发者

Programmatic update of InfoPath form XML is stripping off tags

I have a C# program that is looping through all of the forms (browser enabled) in my forms library and injecting an XML node into each of them (for a newly promoted field). For some reason, when the XML is saved back to the form, the first few tags are being stripped off. Specifically, these tags are:

<?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?>"

My code to update the XML is as follows:

    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;
    }

The addition of the new node is working properly; I can see that the new XML is properly formed. It's just that the tags get stripped off once the file is loaded from the MemoryStream into the XmlDocument object. And once these tags are missing, the forms won't open anymore in IP.

PLEASE HELP!

Thank you!


Change the line which reads:

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

to read:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜