InfoPath中repeating section中赋值操作

简介:

最 近项目需要自定义开发InfoPath Template,之前项目中只需要修改一些字段,所以觉得还是比较简单。只是InfoPath调试环境真的很不方便,必须每次开发好的InfoPath 发布到SharePoint Server,然后在在线打开,调用本地InfoPath2007/InfoPath 2010/InfoPath2013的程序打开。所以这样想看一下自己开发的结果,还是要等几分钟。不知道InfoPath有本地的 Server,Visual Studio调试可以直接本地打开.xsn文件。

功能需求如下(已经简化了),程序后台加载一个xml文件,然后点击”生成”,就把对应的数据填充到userName的文本框中。文本框使用的TextBox控件。

xml的结构如下:

<root>
<persons>
<person>
<username>aaa</username>
<gender>man</gender>
<age>20</age>
<email>test@hotmail.com</email>
</person>

<person>
<username>aaa</username>
<gender>man</gender>
<age>20</age>
<email>test@hotmail.com</email>
</person>
</persons>
</root>

InfoPath Template局部界面如下:

image

用来绑定的控件的Main Source结构如下,每个重复的Section绑定的是order节点,就是说这个InfoPath模板生成多个order节点。然后我们需要从从加载过来的xml,把username赋值给cutomername。

<root>
<orders>
<order>
<customername></customername>
<products>
<product>
<quantity></quantity>
<unitprice></unitprice>
</product>
</products>
</order>
<order>
<customername></customername>
<products>
<product>
<quantity></quantity>
<unitprice></unitprice>
</product>
</products>
</order>
</orders>
</root>

最开始的代码是,“生成”按钮的点击事件。事件内部代码。

XmlDocument xmlDoc=new XmlDocument();xmlDoc.load("http://www.example.com/source.xml");XmlElment root=xmlDoc.DocumentElement;string userName=root.SelectSingleNode("/persons/person/username").text;thisXDocument.DOM.selectSingleNode("/orders/order/customername").text=userName;

出 现的问题,当用户创建一个新的section,点击“生成”按钮,还只是改变第一个section里面的username文本框的值。这是因为 thisXDocument.DOM代表的是:获取一个对表单的基础 XML 文档的引用,采用 XML 文档对象模型 (DOM) 形式。所以上面写的XPath只是针对xml中的第一个绑定数据进行更改。需要修改代码,需要知道用户当前是点击了哪个section的“生成”按钮。其 实我的思路被固定了,其实在点击“生成”按钮时,会传入一个DomEvent e参数给方法体。这个通过e.Source,我们可以取得当前section所对应的数据源。也就是你重复绑定的那个节点,在这里就是order。所以代 码需要修改如下,才能在每个指定的section,点击“生成”按钮可以赋值给当前section的username文本框中。

XmlDocument xmlDoc=new XmlDocument(); xmlDoc.load("http://www.example.com/source.xml"); XmlElment root=xmlDoc.DocumentElement; string userName=root.SelectSingleNode("/persons/person/username").text; e.Source.DOM.selectSingleNode("/customername").text=userName;//当前最内部重复绑定的节点是order,所以xpath就是“/customername”。

 

小 结:在InfoPath开发中,很多方法和事件不同Windows Form里面的编程理念。很多情况下InfoPath开发更多是让开发者去绑定数据,定义xml结构和xsd结构,然后更快的加载数据。在代码内部就是使 用XPath,C#操作XML,然后进行数据的显示和生成。这也是早期类似InfoPath技术为什么很火的原因,开发起来相对快速,并且最后生成的数据 是xml格式。在电子商务等平台上,XML具有先天的优势。但是发展了这么多年,完全架构XML技术的产品不多了,估计InfoPath属于古董级产品 了。


目录
相关文章
|
3月前
|
XML C# 数据格式
The data at the root level is invalid. Line 1, position 1.
The data at the root level is invalid. Line 1, position 1.
21 0
|
8月前
|
JSON 数据格式
成功解决 global init errTypeError:Right-hand side of 'instanceof' is not callable
成功解决 global init errTypeError:Right-hand side of 'instanceof' is not callable
TestRange.cs error CS0104: `Range' is an ambiguous reference between `System.Range' and Gtk.Range
TestRange.cs error CS0104: `Range' is an ambiguous reference between `System.Range' and Gtk.Range
136 0
SAP WM初阶Storage Type上的SUM Indicator参数修改
SAP WM初阶Storage Type上的SUM Indicator参数修改
SAP WM初阶Storage Type上的SUM Indicator参数修改
|
程序员 编译器 C语言
解决办法:undefined reference to symbol '_ZTVN10__cxxabiv117__class_type_infoE@@CXXABI_1.3
解决办法:undefined reference to symbol '_ZTVN10__cxxabiv117__class_type_infoE@@CXXABI_1.3
762 0
|
NoSQL Redis