我正在使用Lists 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
答案 0 :(得分:2)
我从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;
}
这具有处理分隔符的优点;#如果它出现(看起来不太可能)在值部分中。
它还具有以下优点
希望这有帮助。
答案 1 :(得分:1)
SharePoint中的查找字段值包含两条信息 - 正在查找的项目的ID以及引用字段的文本值。使用;#
作为分隔符将它们组合在一个字符串中。
你在这里得到的是SPFieldLookupMulti
- 字段的值,你可以在这里选择多个值。因此,它包含ID1;#value1;#ID2; #value2 ...
最简单的解决方案是String.Split
向上;#
子字符串(请参阅此响应以了解如何:https://stackoverflow.com/q/1126933/239599)然后只访问结果数组的偶数索引:第0个元素包含ID1,第一个元素包含value1;第二个元素包含ID2;第3个元素包含value2。
因此,您可以使用for
循环并将计数器增加2。
答案 2 :(得分:-1)
最好的方法是:
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>";
}
}