用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();
}
分类:
Office
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
2009-07-27 如何控制列表视图栏位的宽度?