?
Solved

InfoPath and XML Problem

Posted on 2011-04-27
3
Medium Priority
?
1,274 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

0
Comment
Question by:yechan
  • 2
3 Comments
 
LVL 28

Accepted Solution

by:
clayfox earned 2000 total points
ID: 35478826
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
 
LVL 1

Author Comment

by:yechan
ID: 35479047
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
 
LVL 1

Author Comment

by:yechan
ID: 35485266
I am gonna close this question.  I think I have figured it out.  

Thanks for your input.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Note:  There are two main ways to deploy InfoPath forms:  Server-side and directly through the SharePoint site.  Deploying a server-side InfoPath form means the form is approved by the Administrator, thus allowing greater functionality in the form. …
There is one common problem that all we SharePoint developers share: custom solution deployment. This topic can't be covered fully in this short article, so all I want to do in this one is to review it from a development-to-operations perspectiv…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month16 days, 21 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question