Programmatically create InfoPath documents

A

Andrew

I am collecting data though a C# program and I would like to be able to
programmatically create an InfoPath form. I found a way to do it in JScript
by using an ActiveXObject like this ...

var oApp = new ActiveXObject("InfoPath.Application");
var oXDocument =
oXDocumentCollection.NewFromSolution("http://server/sites/MySite/PurchaseOrder/Forms/template.xsn");
oXDocument.DOM.selectSingleNode("//ID").text = "something";

I would like to be able to do all of that but in C#

Does anyone know a way that doesn't Involve downloading the xsn file
unpacking it in order to get the template.xml file out?

I have tried both of these dlls with no luck.

Microsoft.Office.Interop.InfoPath.SemiTrust.dll
Microsoft.Office.Interop.InfoPath.dll

Thanks,

Andrew
 
B

Boris Kleynbok

You can generate form XML template by opening xsn then saving blank form.
Then you can load that up as XmlDocument and work with XmlDocument by
accessing elements using a combination of DOM and XPath and populate InnerXml
or InnerText from whatever datasources you choose. I am working with
Strongly Typed Datasets in VS2005.

Quick sample on how you can load XML from file into XmlDocument
xml_string is the location of your file:

XmlTextReader xtr = new XmlTextReader(new StringReader(xml_string));
xtr.Read();
xmlDocument.Load(xtr);

This is how you could access the Xml elements:
XmlNodeList lisp = doc.GetElementsByTagName("mstns:LisPendantsList");

this would get you the top complex type elements and by accessing and
scrolling through XmlNodeList you can access InnerXml or InnerText property.

Loading blank form will save you the hassle in dealing with namespaces.

Hope this helps.
 
A

Andrew

The only fear I have in doing that is if the user drammically changes the
xsn form without saving a new xml template or really if they change anything
at all since the namespace will be different now between the template xml
file and the XSN file.

NameSpace manager isn't all that bad. Since I really knew nothing about
XML before InfoPath it did take a little bit, but here is some code for
getting the namespace manager and prefix out of the InfoPath xml doc. It's
in C#

#region Get name space
public XmlNamespaceManager GetNamespaces(XmlDocument xmlDoc)
{
XmlNode node1 = xmlDoc.DocumentElement;
XmlNamespaceManager manager1 = new
XmlNamespaceManager(xmlDoc.NameTable);
int num1 = 4;
XmlNode node2 = node1.Attributes.Item(num1);
string text1 = node2.LocalName;
string text2 = node1.Attributes.Item(num1).Value;
manager1.AddNamespace(text1,text2);
return manager1;
}
#endregion

Thanks,

Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top