gpt4 book ai didi

xml - 您如何处理 fetchxml 结果数据?

转载 作者:数据小太阳 更新时间:2023-10-29 01:47:38 24 4
gpt4 key购买 nike

我一直避免使用 fetchxml,因为我不确定调用 crmService.Fetch(fetchXml) 后处理结果数据的最佳方式。在一些情况下,我使用带有 LINQ 的 XDocument 从该数据结构中检索数据,例如:

XDocument resultset = XDocument.Parse(_service.Fetch(fetchXml));
if (resultset.Root == null || !resultset.Root.Elements("result").Any())
{
return;
}
foreach (var displayItem in resultset.Root.Elements("result").Select(item => item.Element(displayAttributeName)).Distinct())
{
if (displayItem!= null && displayItem.Value != null)
{
dropDownList.Items.Add(displayItem.Value);
}
}

处理fetchxml结果数据的最佳方式是什么,以便于使用。将这些记录传递到 ASP.NET 数据网格等应用程序将非常有用。

最佳答案

我喜欢 FetchXML 的灵 active ,因此我开发了以下函数来返回用于绑定(bind)到网格和中继器等的数据表。

        /// <summary>
/// Takes a CRM FetchXML query and returns a DataTable
/// </summary>
/// <param name="fetchXml">The FetchXML query</param>
/// <param name="requiredFields">A array of columns you'd expect returned. This is required as if there is no data for a field/column CRM will not return it which could impact databinding</param>
/// <returns>A datatable containing the results of the FetchXML</returns>
public static DataTable FetchXML2DataTable(string fetchXml, string[] requiredFields)
{
CrmService tomService = new CrmService();
tomService = CrmWebService;

string result = tomService.Fetch(fetchXml);
DataSet ds = new DataSet();

System.IO.StringReader reader = new System.IO.StringReader(result);
ds.ReadXml(reader);

DataTable dt = ds.Tables[1];

//check all required columns are present otherwise add them to make life easier for databinding at the top level
//caused by CRM not returning fields if they contain no data
foreach (string field in requiredFields)
{ //Check for column names and nested tables
if ((dt.Columns.IndexOf(field) < 0) && (dt.DataSet.Tables.IndexOf(field) <0))
{
//Add column to datatable even though it is empty for reason stated above
dt.Columns.Add(new DataColumn(field));
}
}

return dt;
}

requiredFields 字符串数组在那里,因为如果您的结果集不包含该列的数据,则不会返回列,但是出于绑定(bind)到数据网格等的确切原因,我可能希望该列就位。

CrmService 是一个启动网络服务的单例类。

希望对你有用。

关于xml - 您如何处理 fetchxml 结果数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1210075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com