Programmatic XML update is stripping off InfoPath tags - InfoPath Dev
in

InfoPath Dev

Programmatic XML update is stripping off InfoPath tags

Last post 10-11-2010 07:57 AM by Jimmy. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 10-07-2010 02:28 PM

    Programmatic XML update is stripping off InfoPath tags

    Hello,

     I have a C# program that loops through all of the forms in my form library and injects an XML node where it's missing.  The problem is that once the xml is saved, the first few tags that are in every one of my IP forms are getting stripped off.  Specifically, these tags are being removed:

    <?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.1081" 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:Contractor_Setup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:tns="http://schemas.microsoft.com/sharepoint/soap/directory/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:ns1="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-10T18:19:55" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-US">
     

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

     

    I'm not having a problem with the parameters being passed in.  The SPListItem is coming from my forms library.  The other parameters are:

    nodeToUpdateStr = "/my:ConfirmationControls"
    nodeToInsertStr = "my:DisableAccessSent"
    nodeInnerXmlStr = "FALSE"
    firstNode = "/my:Contractor_Setup"

     PLEASE HELP!!  I have no idea why these tags are being stripped off.  Once they're stripped, the form can no longer be opened in InfoPath.

     Thanks!

  • 10-11-2010 07:57 AM In reply to

    Re: Programmatic XML update is stripping off InfoPath tags

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

    Use xmlDoc.InnerXml instead.  That should solve the problem.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
Page 1 of 1 (2 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.