Quick Tip: How to create a Stream from an InfoPath form

by S.Y.M. Wong-A-Ton

For those times when you want to take the XML of an InfoPath form and create a Stream object from it, you can use the MemoryStream class for this as shown in the following C# code:

XPathNavigator root = MainDataSource.CreateNavigator();
string formXml = root.SelectSingleNode(
    "my:myFields", NamespaceManager).OuterXml;
System.IO.MemoryStream ms =
    new System.IO.MemoryStream(
    System.Text.Encoding.UTF8.GetBytes(formXml));

Or you can use the following Visual Basic code:

Dim root As XPathNavigator = MainDataSource.CreateNavigator()
Dim formXml As String = root.SelectSingleNode( _
    "my:myFields", NamespaceManager).OuterXml
Dim ms As System.IO.MemoryStream = _
    New System.IO.MemoryStream( _
    System.Text.Encoding.UTF8.GetBytes(formXml))

where my:myFields is the root node of the InfoPath form.

 Subscribe (RSS | Email)

Related Posts

Comments to this post were closed 30 days after it was published.

Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent of S.Y.M. Wong-A-Ton. Usage of techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author, S.Y.M. Wong-A-Ton.

Working with InfoPath