接收 System.FormatException:将值添加到字符串以获取 XML 时的索引(从零开始)
Receiving System.FormatException: Index (zero based) when adding values to a string for Fetch XML
我正在构建一个包含分页 cookie 的提取 XML 字符串,完成页面上记录的更新并继续下一个。
我使用以下内容构建提取:
获取
string fetchquery1 = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' count='1' page ='";
string fetchquery2 = @"' paging-cookie='";
string fetchquery3= @"'> <entity name='contact'>
<attribute name='fullname' />
<attribute name='donotbulkemail' />
<order attribute='fullname' descending='false' />
<filter type='and'>
<condition attribute='donotbulkemail' operator='eq' value='0' />
</filter>
</entity>
</fetch>";
string pagingcookie = "";
int page = 0;
然后在 while 循环开始时,我递增页面并构建提取:
while (true) {
//increment the page
page++;
//set the fetch
string updatedfetchquery = string.Format(fetchquery1 + page + fetchquery2 + pagingcookie + fetchquery3);
返回第一个结果后,我抓取 cookie 并清理它的格式
pagingcookie = accounts.PagingCookie;
if (pagingcookie.Contains("<"))
{
pagingcookie = pagingcookie.Replace("<", "<");
pagingcookie = pagingcookie.Replace("\"", """);
pagingcookie = pagingcookie.Replace(">", ">");
}
A console.write
向我展示了在字符串中添加的 cookie 的正确格式
<cookie page="1"><fullname last="=Vena Winton" first="=Vena Winton" /><contactid last="{0BB28D30-BD77-E711-810B-E0071B66F061}" first="{0BB28D30-BD77-E711-810B-E0071B66F061}" /></cookie>
我发现第一页执行成功,第二页执行失败,如下:
System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
在构建提取 xml 字符串的行上。
如果我删除获取字符串中的分页 cookie 引用,那么代码将继续愉快地执行,直到 Microsoft Dynamics 确定我有足够的页面而不使用分页 cookie。
完整代码如下:
string fetchquery1 = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' count='1' page ='";
string fetchquery2 = @"' paging-cookie='";
string fetchquery3= @"'> <entity name='contact'>
<attribute name='fullname' />
<attribute name='donotbulkemail' />
<order attribute='fullname' descending='false' />
<filter type='and'>
<condition attribute='donotbulkemail' operator='eq' value='0' />
</filter>
</entity>
</fetch>";
string pagingcookie = "";
int page = 0;
while (true) {
//increment the page
page++;
//set the fetch
string updatedfetchquery = string.Format(fetchquery1 + page + fetchquery2 + pagingcookie + fetchquery3);
Console.WriteLine(page);
{
var multipleRequest = new Microsoft.Xrm.Sdk.Messages.ExecuteMultipleRequest()
{
Settings = new ExecuteMultipleSettings()
{
ContinueOnError = false,
ReturnResponses = true
},
Requests = new OrganizationRequestCollection()
};
Console.WriteLine(updatedfetchquery);
EntityCollection accounts = service.RetrieveMultiple(new FetchExpression(updatedfetchquery));
foreach (var c in accounts.Entities)
{
Microsoft.Xrm.Sdk.Messages.UpdateRequest updateRequest = new Microsoft.Xrm.Sdk.Messages.UpdateRequest { Target = c };
multipleRequest.Requests.Add(updateRequest);
c.Attributes["donotbulkemail"] = true;
//Console.WriteLine("accountid: {0}", c.Attributes["donotbulkemail"]);
Console.WriteLine(updatedfetchquery);
// Console.WriteLine(page);
pagingcookie = accounts.PagingCookie;
if (pagingcookie.Contains("<"))
{
pagingcookie = pagingcookie.Replace("<", "<");
pagingcookie = pagingcookie.Replace("\"", """);
pagingcookie = pagingcookie.Replace(">", ">");
}
}
Microsoft.Xrm.Sdk.Messages.ExecuteMultipleResponse multipleResponse = (Microsoft.Xrm.Sdk.Messages.ExecuteMultipleResponse)service.Execute(multipleRequest);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
}
}
}
我们定义了一个 GetAll 辅助方法,它可以为您处理所有分页 cookie 的复杂性
public static List<Entity> GetAll(IOrganizationService service, QueryExpression query)
{
var entities = new List<Entity>();
query.PageInfo = new PagingInfo();
query.PageInfo.Count = 5000;
query.PageInfo.PagingCookie = null;
query.PageInfo.PageNumber = 1;
var res = service.RetrieveMultiple(query);
entities.AddRange(res.Entities);
while (res.MoreRecords == true)
{
query.PageInfo.PageNumber++;
query.PageInfo.PagingCookie = res.PagingCookie;
res = service.RetrieveMultiple(query);
entities.AddRange(res.Entities);
}
return entities;
}
虽然这是为与 QueryExpressions 一起使用而设计的,但您可以使用 SDK FetchXmlToQueryExpressionRequest 方法轻松地将 FetchXML 查询转换为查询表达式。
我正在构建一个包含分页 cookie 的提取 XML 字符串,完成页面上记录的更新并继续下一个。
我使用以下内容构建提取: 获取
string fetchquery1 = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' count='1' page ='";
string fetchquery2 = @"' paging-cookie='";
string fetchquery3= @"'> <entity name='contact'>
<attribute name='fullname' />
<attribute name='donotbulkemail' />
<order attribute='fullname' descending='false' />
<filter type='and'>
<condition attribute='donotbulkemail' operator='eq' value='0' />
</filter>
</entity>
</fetch>";
string pagingcookie = "";
int page = 0;
然后在 while 循环开始时,我递增页面并构建提取:
while (true) {
//increment the page
page++;
//set the fetch
string updatedfetchquery = string.Format(fetchquery1 + page + fetchquery2 + pagingcookie + fetchquery3);
返回第一个结果后,我抓取 cookie 并清理它的格式
pagingcookie = accounts.PagingCookie;
if (pagingcookie.Contains("<"))
{
pagingcookie = pagingcookie.Replace("<", "<");
pagingcookie = pagingcookie.Replace("\"", """);
pagingcookie = pagingcookie.Replace(">", ">");
}
A console.write
向我展示了在字符串中添加的 cookie 的正确格式
<cookie page="1"><fullname last="=Vena Winton" first="=Vena Winton" /><contactid last="{0BB28D30-BD77-E711-810B-E0071B66F061}" first="{0BB28D30-BD77-E711-810B-E0071B66F061}" /></cookie>
我发现第一页执行成功,第二页执行失败,如下:
System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
在构建提取 xml 字符串的行上。
如果我删除获取字符串中的分页 cookie 引用,那么代码将继续愉快地执行,直到 Microsoft Dynamics 确定我有足够的页面而不使用分页 cookie。
完整代码如下:
string fetchquery1 = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' count='1' page ='";
string fetchquery2 = @"' paging-cookie='";
string fetchquery3= @"'> <entity name='contact'>
<attribute name='fullname' />
<attribute name='donotbulkemail' />
<order attribute='fullname' descending='false' />
<filter type='and'>
<condition attribute='donotbulkemail' operator='eq' value='0' />
</filter>
</entity>
</fetch>";
string pagingcookie = "";
int page = 0;
while (true) {
//increment the page
page++;
//set the fetch
string updatedfetchquery = string.Format(fetchquery1 + page + fetchquery2 + pagingcookie + fetchquery3);
Console.WriteLine(page);
{
var multipleRequest = new Microsoft.Xrm.Sdk.Messages.ExecuteMultipleRequest()
{
Settings = new ExecuteMultipleSettings()
{
ContinueOnError = false,
ReturnResponses = true
},
Requests = new OrganizationRequestCollection()
};
Console.WriteLine(updatedfetchquery);
EntityCollection accounts = service.RetrieveMultiple(new FetchExpression(updatedfetchquery));
foreach (var c in accounts.Entities)
{
Microsoft.Xrm.Sdk.Messages.UpdateRequest updateRequest = new Microsoft.Xrm.Sdk.Messages.UpdateRequest { Target = c };
multipleRequest.Requests.Add(updateRequest);
c.Attributes["donotbulkemail"] = true;
//Console.WriteLine("accountid: {0}", c.Attributes["donotbulkemail"]);
Console.WriteLine(updatedfetchquery);
// Console.WriteLine(page);
pagingcookie = accounts.PagingCookie;
if (pagingcookie.Contains("<"))
{
pagingcookie = pagingcookie.Replace("<", "<");
pagingcookie = pagingcookie.Replace("\"", """);
pagingcookie = pagingcookie.Replace(">", ">");
}
}
Microsoft.Xrm.Sdk.Messages.ExecuteMultipleResponse multipleResponse = (Microsoft.Xrm.Sdk.Messages.ExecuteMultipleResponse)service.Execute(multipleRequest);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
}
}
}
我们定义了一个 GetAll 辅助方法,它可以为您处理所有分页 cookie 的复杂性
public static List<Entity> GetAll(IOrganizationService service, QueryExpression query)
{
var entities = new List<Entity>();
query.PageInfo = new PagingInfo();
query.PageInfo.Count = 5000;
query.PageInfo.PagingCookie = null;
query.PageInfo.PageNumber = 1;
var res = service.RetrieveMultiple(query);
entities.AddRange(res.Entities);
while (res.MoreRecords == true)
{
query.PageInfo.PageNumber++;
query.PageInfo.PagingCookie = res.PagingCookie;
res = service.RetrieveMultiple(query);
entities.AddRange(res.Entities);
}
return entities;
}
虽然这是为与 QueryExpressions 一起使用而设计的,但您可以使用 SDK FetchXmlToQueryExpressionRequest 方法轻松地将 FetchXML 查询转换为查询表达式。