格式化查找值,从WebServices检索
本文关键字:WebServices 检索 查找 格式化 | 更新日期: 2023-09-27 18:15:50
我正在使用列表WebService和检索XML数据,我需要将其格式化为美观干净的格式,以便在前端显示。我得到的循环值如下格式
12;#Infor ERP Baan;#15;#Infor ERP LN;#31;#Infor PM;#32;#Infor SCM
,我需要将其显示为一个项目列表,因为我需要将值用";"分隔,然后我可以输入for循环并添加<li>
,例如
Infor ERP Baan;Infor ERP LN;Infor PM;Infor SCM
当从SharePoint返回查找数据时,我使用了以下函数和正则表达式来拆分数据。
static private Dictionary<int,string> GetValues(string productsCellData)
{
// regular expression to split the data into an array, we need the ExplictCapture
// to prevent c# capturing the ;#
var regex = new Regex(@"((?<='d);#|;#(?='d))", RegexOptions.ExplicitCapture);
// our array of data that has been processed.
var productsCellsArray = regex.Split(productsCellData);
Dictionary<int, string> productsDictionary = new Dictionary<int, string>();
if (productsCellsArray.Length % 2 == 1)
{
// handle badly formatted string the array length should always be an even number.
}
// set local variables to hold the data in the loop.
int productKey = -1;
string productValue = string.Empty;
// loop over the array and create our dictionary.
for (var i = 0; i < productsCellsArray.Length; i++)
{
var item = productsCellsArray[i];
// process odd/even
switch (i % 2)
{
case 0:
productKey = Int32.Parse(item);
break;
case 1:
productValue = item;
if (productKey > 0)
{
productsDictionary.Add(productKey, productValue);
productKey = -1;
productValue = string.Empty;
}
break;
}
}
return productsDictionary;
}
这样做的好处是,如果分隔符#出现在值部分(看起来不太可能),可以处理它。
它还有以下优点
- 从Id查找值
- 从字典中获取Id的数组
- 从字典中获取值数组
- 检查字典中是否存在值
- 检查字典中是否存在id
SharePoint中的查找字段值包含两条信息——被查找项的ID和被引用字段的文本值。使用;#
作为分隔符将它们组合在一个字符串中。你在这里有一个SPFieldLookupMulti
的值-字段,你可以有多个值选择的时刻。因此,它包含ID1;#value1;#ID2;#value2…
最简单的解决方案是通过;#
子字符串String.Split
向上(参见此响应以找出,如何:https://stackoverflow.com/q/1126933/239599)然后只访问结果数组的偶数索引:第0个元素包含ID1,第1个元素包含value1;第二个元素包含ID2;第三个元素包含value2。
因此,您可以使用for
循环并将计数器增加2。
最好的方法是:
ProductsCellData = "12;#Infor ERP Baan;#15;#Infor ERP LN;#31;#Infor PM;#32;#Infor SCM"
string[] nProductsCellData = Regex.Replace(ProductsCellData, @"'d", ";").Replace(";#", "").Replace(";;", ";").Split(';');
foreach (string product in nProductsCellData)
{
if (product != "")
{
e.Row.Cells[i].Text += "<li>" + product + "</li>";
}
}