We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

InfoPath and XML Problem

yechan
yechan asked
on
Medium Priority
1,387 Views
Last Modified: 2012-05-11
Hi,

I have written a web part that reads an XML file and upon uploading will create an InfoPath on the fly.  The uploading seems to work pretty good, however, when trying to view the resulting InfoPath form, I am presented with an error complaining about an invalid character.  Please see the attached code snippets.

I don't think it's data related because I don't see any invalid XML characters that need to be escaped.  Maybe it's somehow the way I am putting the XML together.

Perhaps another set of eyes will help.

Thanks
public void  UploadDocument()
        {
            byte[] infoPathFormData = null;

            using (MemoryStream ms1 = new MemoryStream())
            {
                using (XmlTextWriter writer = new XmlTextWriter(ms1, System.Text.Encoding.UTF8))
                //using (XmlTextWriter writer = new XmlTextWriter(@"C:\output.xml", System.Text.Encoding.UTF8))
                {
                    // Define the namespace for the form
                    string myNamespace = "http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-04-11T15:45:33";

                    writer.WriteStartDocument();

                    // Create the required processing instructions
                    writer.WriteProcessingInstruction("mso-infoPathSolution", "name=\"urn:schemas-microsoft-com:office:infopath:ProgCreateForm:-myXSD-2007-09-02T01-11-44\" solutionVersion=\"1.0.0.2\" productVersion=\"12.0.0.0\" PIVersion=\"1.0.0.0\" href=\"http://localhost/sites/DBOPS/SomeLibrary/Forms/template.xsn\"");
                    writer.WriteProcessingInstruction("mso-application", "progid=\"InfoPath.Document\" versionProgid=\"InfoPath.Document.2\"");

                    // Create the XML of the main data source of the form
                    writer.WriteStartElement("my", "myFields", myNamespace);
                    WriteXMLElement(writer, "TypRpt", _fields.TypRpt);
                    WriteXMLElement(writer, "CertNo", _fields.CertNo);
                    WriteXMLElement(writer, "BrDate", DateTime.Parse(_fields.BrDate.ToString()).ToShortDateString());
                    WriteXMLElement(writer, "BrDisTm", _fields.BrDisTm);
                    WriteXMLElement(writer, "NmeRpt", _fields.NmeRpt);
                    WriteXMLElement(writer, "BrDisDtd", DateTime.Parse(_fields.BrDisDtd.ToString()).ToShortDateString());
                    WriteXMLElement(writer, "Title", _fields.Title);
                    WriteXMLElement(writer, "PhnNo", _fields.PhnNo);
                    WriteXMLElement(writer, "CmpCom", _fields.CmpCom);
                    WriteXMLElement(writer, "OrgBr", _fields.OrgBr);
                    WriteXMLElement(writer, "EmlAdd", _fields.EmlAdd);
                    WriteXMLElement(writer, "GovCiv", _fields.GovCiv);
                    WriteXMLElement(writer, "OthSpc", _fields.OthSpc);
                    WriteXMLElement(writer, "MilRet", _fields.MilRet);
                    WriteXMLElement(writer, "MilAct", _fields.MilAct);
                    WriteXMLElement(writer, "Othr", _fields.Othr);
                    WriteXMLElement(writer, "TotAff", _fields.TotAff);
                    WriteXMLElement(writer, "MilDep", _fields.MilDep);
                    WriteXMLElement(writer, "GovCon", _fields.GovCon);
                    WriteXMLElement(writer, "MemPub", _fields.MemPub);
                    writer.WriteEndElement();
                    writer.WriteEndDocument();
                    writer.Flush();
                    writer.Close();
                }

                infoPathFormData = ms1.GetBuffer();
                ms1.Close();
                ms1.Flush();
                ms1.Dispose();   
            }

            // Upload the newly created InfoPath form to SharePoint
            if (infoPathFormData != null && infoPathFormData.Length != 0)
            {
                using (WebClient client = new WebClient())
                {
                    // Set the credentials to be used for upload to SharePoint
                    client.Credentials = CredentialCache.DefaultNetworkCredentials;

                    //Upload the newly created form to a SharePoint form library
                    client.UploadData(
                    @"http://localhost/sites/DBOPS/SomeLibrary/test7.xml",
                    "PUT",
                    infoPathFormData);

                    lblErr.Text = "done.   size of data: " + infoPathFormData.Length.ToString();

                    client.Dispose();
                }
            }
        }

        private void WriteXMLElement(XmlTextWriter writer, string element, string value)
        {
            string myNamespace = "http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-04-11T15:45:33";

            writer.WriteStartElement("my", element, myNamespace);
            writer.WriteString(value);
            writer.WriteEndElement();
        }

Open in new window

The form contains XML that cannot be parsed:
An invalid character was found in text content.

Line 1, Position 1076
<?xml version="1.0" encoding="utf-8"?><?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:ProgCreateForm:-myXSD-2007-09-02T01-11-44" solutionVersion="1.0.0.2" productVersion="12.0.0.0" PIVersion="1.0.0.0" href="http://ehqmcstage/sites/DBOPS/SSRS/PIIBreachLibrary/Forms/template.xsn"?><?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.2"?><my:myFields xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-04-11T15:45:33"><my:TypRpt>Testing1</my:TypRpt><my:CertNo>1234b</my:CertNo><my:BrDate>4/12/2011</my:BrDate><my:BrDisTm>1300</my:BrDisTm><my:NmeRpt>Ye-Chan Chapman</my:NmeRpt><my:BrDisDtd>4/11/2011</my:BrDisDtd><my:Title>software Deveoper</my:Title><my:PhnNo>571.291.1531</my:PhnNo><my:CmpCom>Twinsoft</my:CmpCom><my:OrgBr>Mairns</my:OrgBr><my:EmlAdd>ylc6232yahoo.com</my:EmlAdd><my:GovCiv>5</my:GovCiv><my:OthSpc>3</my:OthSpc><my:MilRet>2</my:MilRet><my:MilAct>1</my:MilAct><my:Othr>0</my:Othr><my:TotAff>65</my:TotAff><my:MilDep>5</my:MilDep><my:GovCon>9</my:GovCon><my:MemPub>12</my:MemPub></my:myFields>
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------^

Open in new window

Comment
Watch Question

Director of Business Integration
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
That was a great idea.  After downloading the resulting InfoPath form onto my desktop and opening it up with NotePad++, I get something like the following

......</my:myFields>NULNULNULL.....

I don't know where NUL are coming from.

Author

Commented:
I am gonna close this question.  I think I have figured it out.  

Thanks for your input.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.