现在的位置: 首页 > 综合 > 正文

[转载]4 Ways To Programmatically Add A Row To A Repeating Table In InfoPath

2012年01月23日 ⁄ 综合 ⁄ 共 3208字 ⁄ 字号 评论关闭
原文: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 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 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 anXmlDocumentis 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 1Cell 2Cell 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://www.bizsupportonline.net/infopath2007/programmatically-add-row-repeating-table-xmlwriter.htm

抱歉!评论已关闭.