[Reprinted] 4 ways to programmatically Add a row to a repeating table in infopath

Source: Internet
Author: User
Original article: 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.
Inprogrammatically Add a row to a repeating table using an xmlwriter objecti wrote about how you can use the xmlwriter object to add a row to a repeating table. while waiting of you have adopted this way of adding a row to a repeating table, there are several other ways available, which I'll touch.
All methods to add a row to a repeating table make use of theappendchildmethod of anxpathnavigatorobject. There are 4 overloads forappendchild:
One that accepts a stringone that accepts anxpathnavigatorobjectone that accepts anxmlreaderobjectone that returns anxmlwriterobject
You can use any one of the aforementioned methods forappendchildto 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 inprogrammatically 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 astringbuilderobject and then passed to theappendchildmethod as a string to create the row.
String my = namespacemanager. lookupnamespace ("my ");
Stringbuilder sb = new stringbuilder ();
SB. append (" ");
SB. append (" ");
SB. append ("cell 1 ");
SB. append (" ");
SB. append (" ");
SB. append ("Cell 2 ");
SB. append (" ");
SB. append (" ");
SB. append ("Cell 3 ");
SB. append (" ");
SB. append (" ");
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 anxmlincluentis used to construct the XML for a row and then an xpathnavigator object is created from the document element of thisxmldocumentand passed to theappendchildmethod 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 afilestreamobject is used to read an XML file that contains the XML structure for a row. It then creates anxmlreaderobject from thefilestreamand passes it to theappendchildmethod to create the row.
Contents of a file namedrow. xmlthat is located on the C-drive:
Cell 1 Cell 2 Cell 3
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
Seehttp: // response

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.