It is very convenient to use infopath to reference external data sources and perform data operations, such as connecting to the SQL database and referencing WebServices, XML, and Sharepoint list. However, we found a problem, when infopath references a data source, the data is cached on the client. When the referenced data source is large, the user operation is slow. At this time, we hope that the downloaded data can be small and accurate. We can customize the data source and add parameters to achieve dynamic data source query. In vsta, You can dynamically change the data source query parameters and reverse query results by referring to the following code:
Code
Public void ctrl1_14_clicked (Object sender, clickedeventargs E)
{
// Write code here.
System. xml. XPath. xpathnavigator root;
Root = This. maindatasource. createnavigator ();
Dataconnection Dc = (dataconnection) datasources ["getdata"]. queryconnection); // obtain the data source object
If (DC is webserviceconnection)
{
String queryfield = ""
+ "<TNS: getdata xmlns: TNS = \" http://tempuri.org/\ "> <TNS: secucode>" + root. selectsinglenode ("// NS2. securities code", this. namespacemanager ). value + "</TNS: secucode>"
+ "<TNS: tradingday>" + root. selectsinglenode ("// NS2. query date", this. namespacemanager). Value + "</TNS: tradingday>"
+ "</TNS: getdata>"
+ "";
Xmldocument inputdocument = new xmldocument ();
Inputdocument. loadxml (queryfield );
Xpathnavigator inputnav = inputdocument. createnavigator ();
Xmldocument outputdocument = new xmldocument ();
Xpathnavigator outputnav = outputdocument. createnavigator ();
Microsoft. Office. infopath. webserviceconnection fqc = (Microsoft. Office. infopath. webserviceconnection) DC;
Fqc. Execute (inputnav, outputnav, null); // execute Data Query
// MessageBox. Show (outputnav. outerxml );
String outxml = outputnav. outerxml. Replace ("xmlns = \" http://tempuri.org /\"","");
Outputdocument. loadxml (outxml );
Xmlelement nodeelement = (xmlelement) outputdocument. selectsinglenode ("getdataresponse/getdataresult/securities abbreviation ");
Xpathnavigator setnode = root. selectsinglenode ("// NS2. company name", this. namespacemanager );
Deletenil (setnode );
Setnode. setvalue (nodeelement. innertext); // attaches a value to the infopath Field
Nodeelement = (xmlelement) outputdocument. selectsinglenode ("getdataresponse/getdataresult/transaction date ");
Setnode = root. selectsinglenode ("// NS2. Transaction date", this. namespacemanager );
Deletenil (setnode );
Setnode. setvalue (nodeelement. innertext );
}
}
Public void deletenil (xpathnavigator node)
{
If (node. movetoattribute (
"Nil", "http://www.w3.org/2001/XMLSchema-instance "))
Node. deleteself ();
}