• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1291
  • Last Modified:

InfoPath and XML Problem

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

0
yechan
Asked:
yechan
  • 2
1 Solution
 
Clay FoxDirector of Business IntegrationCommented:
It looks like it is in position 1076.

Could be the datetime parse?

InfoPath has its own nuances from standard XML so just at a matter of tracking it down.
One way is to mock up in InfoPath and then save as XML and compare.
0
 
yechanAuthor 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.
0
 
yechanAuthor Commented:
I am gonna close this question.  I think I have figured it out.  

Thanks for your input.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now