Extracting Attachment from InfoPath form using C#

The sample code for extracting attachment from submitted InfoPath form

var lstTimeSheet = new List<RelatedTimesheet>();
 foreach (var timeSheet in invoiceFormData.Summary.Timesheet)
 {
 if (timeSheet.Attachment != null)
 {
 var tSheet = new RelatedTimesheet();
 var b = timeSheet.Attachment;
 var nameBufferLen = b[20] * 2;
 var fileNameBufffer = new byte[nameBufferLen];
 for (var i = 0; i < nameBufferLen; i++)
 {
 fileNameBufffer[i] = b[24 + i];
 }

 var charFileName = Encoding.Unicode.GetChars(fileNameBufffer);
 var fileName = new string(charFileName);
 tSheet.FileName = fileName.Substring(0, fileName.Length - 1); 

 var fileContent = new byte[b.Length - (24 + nameBufferLen)];
 for (var i = 0; i < fileContent.Length; i++)
 {
 fileContent[i] = b[24 + nameBufferLen + i];
 }

 tSheet.Attachment = fileContent;
 lstTimeSheet.Add(tSheet);
 }
 }

public class RelatedTimesheet
{
 public string FileName { get; set; }
 public byte[] Attachment { get; set; }
}

Using HTML Tags for formatting in InfoPath Rich Text Box Control

Hi,

We had a requirement to format custom error messages that we were displaying in the rich text box control inside InfoPath Form.

For this we can use AppendChild method of XPathNavigator object.

For e.g. below is the html we need

The code that we need to use

XPathNavigator richTextField = domNav.SelectSingleNode(“/my:myFields/my:field4”, NamespaceManager);

richTextField.AppendChild(“<h2 xmlns=\”http://www.w3.org/1999/xhtml\”>Error Message Header</h2><p xmlns=\”http://www.w3.org/1999/xhtml\”>This is my custom <font xmlns=\”http://www.w3.org/1999/xhtml\” size=\”3\” color=\”red\”> error </font> message.</p>”);

The output

We need to define namespace for each tag used else we get exception.

The helpful post

http://www.bizsupportonline.net/infopath2007/how-to-render-html-in-rich-text-box-infopath.htm

Hope it helps..

Serialize and Deserialize InfoPath form and extract the attachment using WebClient in C#

Hi,

Suppose this is our InfoPath form

First we will generate the class for the above InfoPath form

https://nishantrana.me/2014/07/13/generate-a-c-class-from-an-infopath-form/

The sample code


myFields invoice = null;
WebClient webclient = new WebClient();
webclient.UseDefaultCredentials = true;

using (Stream fileStream = webclient.OpenRead("http://server:5000/formlibrary/FirstForm.xml?NoRedirect=true"))
{
if (fileStream != null)
{
XmlTextReader reader = new XmlTextReader(fileStream);

reader.Read();
reader.MoveToContent();

XmlSerializer xser = new XmlSerializer(typeof(myFields));
myFields invoiceInfopath = (myFields)xser.Deserialize(reader);
byte[] b = invoiceInfopath.field2;

// filename
int nameBufferLen = b[20] * 2;
byte[] fileNameBufffer = new Byte[nameBufferLen];

for (int i = 0; i < nameBufferLen; i++)
{
fileNameBufffer[i] = b[24 + i];
}

char[] charFileName = UnicodeEncoding.Unicode.GetChars(fileNameBufffer);
string fileName = new string(charFileName);
fileName = fileName.Substring(0, fileName.Length - 1);

// attchment
byte[] fileContent = new byte[b.Length - (24 + nameBufferLen)];

for (int i = 0; i < fileContent.Length; i++)
{
fileContent[i] = b[24 + nameBufferLen + i];
}

FileStream fs = new FileStream(@"C:\test\" + fileName, FileMode.Create);
fs.Write(fileContent, 0, fileContent.Length);
fs.Close();
}

Hope it helps.

How to Read the InfoPath content and extract the attachment using XMLDocument in C#

The InfoPath form:-

The namespace and the field1 that is node that contains the attachment

Sample Code:


XmlDocument myDoc = new XmlDocument();
myDoc.Load(@"C:\test\form.xml");

// specify the name space
XmlNamespaceManager ns = new XmlNamespaceManager(myDoc.NameTable);
ns.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2014-07-12T15:29:09");

// the node that contains the attachment
XmlNodeList nl = myDoc.SelectNodes("/my:myFields/my:group1/my:group2/my:field1", ns);
foreach (XmlNode n in nl)
{
string s = n.InnerText;
byte[] b = Convert.FromBase64String(s);
int nameBufferLen = b[20] * 2;

// file name buffer to get the filename with extension
byte[] fileNameBufffer = new Byte[nameBufferLen];

for (int i = 0; i < nameBufferLen; i++)
{
fileNameBufffer[i] = b[24 + i];
}
char[] charFileName = UnicodeEncoding.Unicode.GetChars(fileNameBufffer);
string fileName = new string(charFileName);
fileName = fileName.Substring(0, fileName.Length - 1);

// byte array for the remaining content
byte[] fileContent = new byte[b.Length - (24 + nameBufferLen)];

for (int i = 0; i < fileContent.Length; i++)
{
fileContent[i] = b[24 + nameBufferLen + i];
}

FileStream fs = new FileStream(@"C:\test\" + fileName, FileMode.Create);
fs.Write(fileContent, 0, fileContent.Length);
fs.Close();

}

Hope it helps..

 

 

 

 

Generate a C# class from an InfoPath form

To generate a C# class from change the extension to .cab from .xsn.

Open and copy the content of cab file to a separate folder

Open the Visual Studio Developer Command Prompt and run the XSD.exe to generate the class

 

Hope it helps…