最
近項目需要自定義開發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>[email protected]</email>
</person>
</persons>
</root>
infopath template局部界面如下:
用來綁定的控件的main source結構如下,每個重複的section綁定的是order節點,就是說這個infopath模闆生成多個order節點。然後我們需要從從加載過來的xml,把username指派給cutomername。
<orders>
<order>
<customername></customername>
<products>
<product>
<quantity></quantity>
<unitprice></unitprice>
</product>
</products>
</order>
</orders>
最開始的代碼是,“生成”按鈕的點選事件。事件内部代碼。
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屬于古董級産品
了。