宝剑锋从磨砺出,梅花香自苦寒来

博客园 首页 联系 订阅 管理
原文:http://www.bizsupportonline.net/infopath2007/4-way-programmatically-add-row-repeating-table.htm
by S.Y.M. Wong-A-Ton

Learn how to take advantage of the 4 overloads of the AppendChild method to programmatically add rows to repeating tables in InfoPath.

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. 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 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 create a new row in a repeating table. All of the examples in this blog post 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 create the row.

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(
"/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 create the row.

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 create the row.

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:

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 http://www.bizsupportonline.net/infopath2007/programmatically-add-row-repeating-table-xmlwriter.htm

posted on 2009-09-01 16:56  Sean_Zhang  阅读(529)  评论(0编辑  收藏  举报