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

by S.Y.M. Wong-A-Ton | Filed under: InfoPath 2007

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.

Related Posts

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.

InfoPath 2010 Cookbook

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010