4 Ways to programmatically add a row to a repeating table in InfoPath

Learn how to programmatically add a row to a repeating table in InfoPath by taking advantage of the 4 overloads of the AppendChild method of the XPathNavigator class.

ADVERTISEMENTS

In Programmatically add a row to a repeating table using an XmlWriter object I wrote about how you can use the XmlWriter object to add a row to a repeating table in InfoPath. While many of you have adopted this way of adding a row to a repeating table, there are several other ways available, which I'll touch upon.

All methods to add a row to a repeating table in InfoPath make use of the AppendChild method of an XPathNavigator object. There are 4 overloads for AppendChild:

  1. One that accepts a string
  2. One that accepts an XPathNavigator object
  3. One that accepts an XmlReader object
  4. One that returns an XmlWriter object

You can use any one of the aforementioned methods for AppendChild to add a row to a repeating table in InfoPath. All of the examples in this article use the XML schema for a repeating table as described in Programmatically add a row to a repeating table using an XmlWriter object.

Method 1 - Use a string to add a row to a repeating table in Infopath

In the following sample code the XML for the row is constructed using a StringBuilder object and then passed to the AppendChild method as a string to add a row to a repeating table in InfoPath.

string my = NamespaceManager.LookupNamespace("my");
StringBuilder sb = new StringBuilder();
sb.Append("<my:group2 xmlns:my=\"");
sb.Append(my);
sb.Append("\">");
sb.Append("<my:field1 xmlns:my=\"");
sb.Append(my);
sb.Append("\">");
sb.Append("Cell 1");
sb.Append("</my:field1>");
sb.Append("<my:field2 xmlns:my=\"");
sb.Append(my);
sb.Append("\">");
sb.Append("Cell 2");
sb.Append("</my:field2>");
sb.Append("<my:field3 xmlns:my=\"");
sb.Append(my);
sb.Append("\">");
sb.Append("Cell 3");
sb.Append("</my:field3>");
sb.Append("</my:group2>");
MainDataSource.CreateNavigator().SelectSingleNode(
&nsbp; "/my:myFields/my:group1", NamespaceManager).AppendChild(sb.ToString());

Method 2 - Use an XPathNavigator object to add a row to a repeating table in InfoPath

In the following sample code an XmlDocument is used to construct the XML for a row and then an XPathNavigator object is created from the document element of this XmlDocument and passed to the AppendChild method to add a row to a repeating table in InfoPath.

XmlDocument doc = new XmlDocument();
XmlNode group = doc.CreateElement("group2", NamespaceManager.LookupNamespace("my"));

XmlNode field = doc.CreateElement("field1", NamespaceManager.LookupNamespace("my"));
XmlNode node = group.AppendChild(field);
node.InnerText = "Cell 1";

field = doc.CreateElement("field2", NamespaceManager.LookupNamespace("my"));
node = group.AppendChild(field);
node.InnerText = "Cell 2";

field = doc.CreateElement("field3", NamespaceManager.LookupNamespace("my"));
node = group.AppendChild(field);
node.InnerText = "Cell 3";

doc.AppendChild(group);

MainDataSource.CreateNavigator().SelectSingleNode(
  "/my:myFields/my:group1",
  NamespaceManager).AppendChild(doc.DocumentElement.CreateNavigator());

Method 3 - Use an XMLReader object to add a row to a repeating table in InfoPath

In the following sample code a FileStream object is used to read an XML file that contains the XML structure for a row. It then creates an XmlReader object from the FileStream and passes it to the AppendChild method to add a row to a repeating table in InfoPath.

Contents of a file named row.xml that is located on the C-drive:

<my:group2 xmlns:my="the_xml_namespace_of_your_form_template_goes_here">
  <my:field1 xmlns:my="the_xml_namespace_of_your_form_template_goes_here">Cell 1</my:field1>
  <my:field2 xmlns:my="the_xml_namespace_of_your_form_template_goes_here">Cell 2</my:field2>
  <my:field3 xmlns:my="the_xml_namespace_of_your_form_template_goes_here">Cell 3</my:field3>
</my:group2>

Code to add a row to the repeating table in InfoPath:

using (FileStream fs = new FileStream(@"C:\row.xml", FileMode.Open))
{
  using (XmlReader reader = XmlReader.Create(fs))
  {
    MainDataSource.CreateNavigator().SelectSingleNode(
      "/my:myFields/my:group1", NamespaceManager).AppendChild(reader);
    reader.Close();
  }
  fs.Close();
}

Method 4 - Use an XmlWriter object to add a row to a repeating table in Infopath

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

 
 Subscribe for updates via RSS or Email

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. 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, S.Y.M. Wong-A-Ton.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners
InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013
InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners
InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010
InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers
InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services


Related InfoPath Articles:

ADVERTISEMENTS

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services