Thursday, 20 December 2012

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

Method 1 - Use 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:-

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:-

<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:-

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");
writer.WriteEndElement();
writer.Close();
}

No comments:

Post a Comment