深入浅出InfoPath——操作InfoPath元素

本文介绍了如何使用InfoPath操作XML数据,包括查询、增加及删除表单元素的方法。特别针对复杂的RepeatingTable元素提供了详细的代码示例,帮助读者更好地理解和掌握InfoPath表单与XML数据之间的交互。

原理:InfoPath表单作为模板来定义Item中xml数据文件的格式和资源,那么我们在操纵InfoPath数据就等于是操作xml数据文件。

  1. 查询InfoPath表单元素
  2. 增加InfoPath表单元素

先看下普通的XML中使用XPath进行查询的例子

  1. 删除InfoPath表单元素

这里我们以复杂的Repeating Table为例

下面的代码是通过按钮遍历查找Repeating Table中的某域为空的行,并删除一行(多行的情况请参考)。

public void btnDeleteBlankRS_Clicked(object sender, ClickedEventArgs e)
{
//Create an XmlNamespaceManager for ease of referencing namespaces
XmlNamespaceManager ns = this.NamespaceManager;

//Create an XPathNavigator object for the main data source
XPathNavigator xnMain = this.MainDataSource.CreateNavigator();

//Create an XPathNodeIterator object for the repeating table so we can loop thru all the rows in the repeating table
XPathNodeIterator xiRepeatingTable = xnMain.Select("/my:myFields/my:gpRepeatingTable/my:gpRepeatingTableRow", ns);

//Loop thru all repeating table rows
while (xiRepeatingTable.MoveNext())
{
//Create an XPathNodeIterator object for the repeating section nodes within the repeating table
XPathNodeIterator xiRepeatingSection = xiRepeatingTable.Current.Select("my:gpRepeatingSection/my:gpRepeatingSectionRow", ns);

//Loop thru the repeating sections within the current repeating table row
while (xiRepeatingSection.MoveNext())
{
//Create an XPathNavigator object for one of the fields within the Repeating Section
//NOTE: You may need to check more than one field to determine if the repeating section should be removed
XPathNavigator xnRepeatingSectionField = xiRepeatingSection.Current.SelectSingleNode("my:field3", ns);

//See if the field in the repeating section is empty
if (xnRepeatingSectionField.Value == string.Empty)
{
//If so, delete the current repeating section
xiRepeatingSection.Current.DeleteSelf();
}
}
}
}

 

string name = "User2";
string game = "Cube";
XmlNode node
= doc.SelectSingleNode(String.Format("/players/player[name='{0}' and game='{1}']", name, game));
node.ParentNode.RemoveChild(node);

或者

String aaa = "/players/player[name='"+user+"' and game='"+game+"']";
XmlNode node
= docc.SelectSingleNode(aaa);

 

 

 

转载于:https://www.cnblogs.com/mingle/archive/2010/12/31/InfoPath_xml_element.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值