[转载]4 Ways to programmatically add a row to a repeating table in InfoPath
2009-09-01 16:56
405 查看
原文: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:
One that accepts a string
One that accepts an XPathNavigator object
One that accepts an XmlReader object
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.
Contents of a file named row.xml that is located on the C-drive:
Code to add a row to the repeating table:
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:
One that accepts a string
One that accepts an XPathNavigator object
One that accepts an XmlReader object
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相关文章推荐
- 转:How to submit rows of a repeating table in InfoPath to a web service? 如何将重复表中的记录传给Web Service ?
- 转:How to submit the rows of a repeating table in InfoPath to a SharePoint list
- Fast ways in R to get the first row of a data frame grouped by an identifier
- 转:Infopath Repeating Table 如何控制最大行数? Limiting number of repeating tables a user can add
- add "remote-notification" to the list of your supported UIBackgroundModes in your Info.plist.(转载)
- Divide and Conquer (1) -- Kth Largest Element in an Array,Different Ways to Add Parentheses
- How To Add Search Bar in Table View
- [转]How to add new table in NopCommerce
- ora-01653: unable to extend table sys.aud$ by 8192 in tablespac system[转载]
- VC++ Unable to register this add-in because its DllRegisterServer returns an error 解决方案(转载)
- Ten ways to improve the performance of large tables in MySQL--转载
- [Tips] How to Add New field into Shapefile attribute table in ArcGIS ?
- [转载] Moving table to a different filegroup in SQL 2005
- How-to find the SQL that using lots of temp tablespace in Oracle
- LeetCode题解——Different Ways to Add Parentheses
- LeetCode No.241 Different Ways to Add Parentheses
- How to adjust the high watermark in ORACLE 10g -- ALTER TABLE SHRINK
- add new row to data.frame/dataframe
- LeetCode *** 241. Different Ways to Add Parentheses
- Add a table adapter to a typed data set in Visual Studio