用infopath 引用外部数据源,进行数据操作,是一件非常方便的事情,比如连接SQL数据库,引用webServices、xml、sharepoint list等,但是我们发现了一个问题,infopath引用数据源时,会将数据缓存在客户端,当我们引用的数居源数据量大的时候,会产生用户操作缓慢的问题。这个时候我们希望下载下来数据能够,既小又准确。我们能够通过自定义数据源加参数的方法,实现动态的数据源查询。在VSTA中动态改变数据源查询参数与反回查询结果,可以参考如下代码:
代码
public void CTRL1_14_Clicked(object sender, ClickedEventArgs e)
{
// 在此处编写代码。
System.Xml.XPath.XPathNavigator root;
root = this.MainDataSource.CreateNavigator();
DataConnection dc = ((DataConnection)DataSources["GetData"].QueryConnection);//获取数据源对象
if (dc is WebServiceConnection)
{
string queryField = ""
+ "<tns:GetData xmlns:tns=\"http://tempuri.org/\"><tns:SecuCode>" + root.SelectSingleNode("//ns2:证券代码", this.NamespaceManager).Value + "</tns:SecuCode>"
+ " <tns:TradingDay>" + root.SelectSingleNode("//ns2:查询日期", 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);//执行数据查询
//MessageBox.Show(outputNav.OuterXml);
string outXml = outputNav.OuterXml.Replace("xmlns=\"http://tempuri.org/\"", "");
outputDocument.LoadXml(outXml);
XmlElement nodeElement = (XmlElement)outputDocument.SelectSingleNode("GetDataResponse/GetDataResult/证券简称");
XPathNavigator setNode = root.SelectSingleNode("//ns2:公司名称", this.NamespaceManager);
DeleteNil(setNode);
setNode.SetValue(nodeElement.InnerText);//对infopath域进行附值
nodeElement = (XmlElement)outputDocument.SelectSingleNode("GetDataResponse/GetDataResult/交易日期");
setNode = root.SelectSingleNode("//ns2:交易日期", 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();
}
{
// 在此处编写代码。
System.Xml.XPath.XPathNavigator root;
root = this.MainDataSource.CreateNavigator();
DataConnection dc = ((DataConnection)DataSources["GetData"].QueryConnection);//获取数据源对象
if (dc is WebServiceConnection)
{
string queryField = ""
+ "<tns:GetData xmlns:tns=\"http://tempuri.org/\"><tns:SecuCode>" + root.SelectSingleNode("//ns2:证券代码", this.NamespaceManager).Value + "</tns:SecuCode>"
+ " <tns:TradingDay>" + root.SelectSingleNode("//ns2:查询日期", 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);//执行数据查询
//MessageBox.Show(outputNav.OuterXml);
string outXml = outputNav.OuterXml.Replace("xmlns=\"http://tempuri.org/\"", "");
outputDocument.LoadXml(outXml);
XmlElement nodeElement = (XmlElement)outputDocument.SelectSingleNode("GetDataResponse/GetDataResult/证券简称");
XPathNavigator setNode = root.SelectSingleNode("//ns2:公司名称", this.NamespaceManager);
DeleteNil(setNode);
setNode.SetValue(nodeElement.InnerText);//对infopath域进行附值
nodeElement = (XmlElement)outputDocument.SelectSingleNode("GetDataResponse/GetDataResult/交易日期");
setNode = root.SelectSingleNode("//ns2:交易日期", 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();
}