Programmatically add a row to a repeating table using an XmlWriter object

Applies to: InfoPath 2007

Use the XmlWriter object that is returned by the AppendChild method of the XPathNavigator object to add a row to a repeating table.



You want to programmatically add a row to a repeating table by clicking a button.


Use the XmlWriter object that is returned by the AppendChild method of the XPathNavigator object to add a row to a repeating table.


The AppendChild method has 4 overloads, one of which returns an XmlWriter object that you can use to create a row in a repeating table.

Before you can get started with writing code for InfoPath form templates, you have to have your programming environment correctly set up and be able to write C# or Visual Basic .NET code. See the following articles for information on what you must have installed before beginning to write code:

Once you've got your programming environment set up, you can accomplish the functionality as follows:

  1. Design an InfoPath form template as shown in figure 1.

    Figure 1. The InfoPath form template in design mode.

    with a Main data source that resembles the following figure:

    Figure 2. The Main data source of the form template.
  2. Double-click the Add Row button to open its Properties dialog box.
  3. On the Button Properties dialog box, click Edit Form Code to add a Clicked event handler for the button to the form template.
  4. Add the following code to the Clicked event handler of the button:
    string myNamespace = NamespaceManager.LookupNamespace("my");
    using (XmlWriter writer = MainDataSource.CreateNavigator().SelectSingleNode(
    "/my:myFields/my:group1", NamespaceManager).AppendChild())
    writer.WriteStartElement("group2", myNamespace);
    writer.WriteElementString("field1", myNamespace, "Cell 1");
    writer.WriteElementString("field2", myNamespace, "Cell 2");
    writer.WriteElementString("field3", myNamespace, "Cell 3");

    Visual Basic
    Dim myNamespace As String = NamespaceManager.LookupNamespace("my")
    Using writer As XmlWriter = MainDataSource.CreateNavigator().SelectSingleNode( _
    "/my:myFields/my:group1", NamespaceManager).AppendChild()
    writer.WriteStartElement("group2", myNamespace)
    writer.WriteElementString("field1", myNamespace, "Cell 1")
    writer.WriteElementString("field2", myNamespace, "Cell 2")
    writer.WriteElementString("field3", myNamespace, "Cell 3")
    End Using
  5. Save, build, and test your work.

Related InfoPath Articles:


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. The 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.