Introduction
We know that users can fill InfoPath forms manually by typing data into form fields. But, this process can also be automated if we already have form data saved in our database or a file etc. This way, Aspose.Form for .NET also makes it possible for developers to fill InfoPath forms programmatically. Let's take a look that how it can be done.
Filling an InfoPath Form with XML Data
Developers can fill InfoPath forms automatically by using two approaches:
- Using XML String
- Using XML Node Object
Note: You can save form data as String or XmlNode objects in the database or a file etc. and then use that to fill forms automatically.
Using XML String
Aspose.Form for .NET provides XmlFormView class that represents InfoPath form control. Using this control, InfoPath forms are viewed in browser. This control provides XmlDataString property that can be used by developers to fill InfoPath forms with already saved data as shown below in the example.
Note: For the code snippet given below, we assume that form data is stored in an XML file.
Example
[C#]
//Opening connection with the XML file
using (System.IO.StreamReader reader = new System.IO.StreamReader(Server.MapPath(xmlFile)))
{
//Reading and assigning the data of XML file to FormControl
XmlFormView1.XmlDataString = reader.ReadToEnd();
}
[VB.NET]
'Opening connection with the XML file
Dim reader As System.IO.StreamReader = New System.IO.StreamReader(Server.MapPath(xmlFile))
Try
'Reading and assigning the data of XML file to FormControl
XmlFormView1.XmlDataString = reader.ReadToEnd
Finally
CType(reader, IDisposable).Dispose()
End Try
Using XML Node Object
Developers can also fill forms using XmlDataNode property of XmlFormView class as shown below in the example code snippet.
Note: For the code snippet given below, we assume that form data is stored in an XML file.
Example
[C#]
//Opening connection with the XML file
using (System.IO.StreamReader reader = new System.IO.StreamReader(Server.MapPath(xmlFile)))
{
//Preparing the XmlDocument object to load data in XML file
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.PreserveWhitespace = false;
//Loading XML data into XmlDocument object from XML file
doc.Load(new System.Xml.XmlTextReader(reader));
//Normalizing the XML data contained in XmlDocument object
doc.Normalize();
XmlFormView1.XmlDataNode = doc.DocumentElement;
}
[VB.NET]
'Opening connection with the XML file
Dim reader As System.IO.StreamReader = New System.IO.StreamReader(Server.MapPath(xmlFile))
Try
'Preparing the XmlDocument object to load data in XML file
Dim doc As System.Xml.XmlDocument = New System.Xml.XmlDocument
doc.PreserveWhitespace = False
'Loading XML data into XmlDocument object from XML file
doc.Load(New System.Xml.XmlTextReader(reader))
'Normalizing the XML data contained in XmlDocument object
doc.Normalize
'Assigning the data of XmlDocument object to FormControl
XmlFormView1.XmlDataNode = doc.DocumentElement
Finally
CType(reader, IDisposable).Dispose()
End Try