我正在处理列表WebService和检索XML数据,我需要格式化它在一个良好和干净的格式,以便在前端显示。我正在获取如下格式的loopup的值
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发布于 2013-03-09 02:31:02
在从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;
}这样做的好处是,如果分隔符;#出现在value部分(看起来不太可能),则可以处理该分隔符。
它还具有以下优点
希望这能有所帮助。
发布于 2011-11-26 04:29:08
SharePoint中的查阅字段值包含两条信息-被查找项的ID和引用字段的文本值。使用;#作为分隔符,将它们组合在一个字符串中。这里有一个SPFieldLookupMulti字段的值,在这个字段中,您可以同时选择多个值。因此,它包含ID1;#value1;#ID2;#value2...
最简单的解决方案是按how子串进行String.Split up (查看此响应以了解如何:https://stackoverflow.com/q/1126933/239599),然后仅访问结果数组的偶数索引:第0个元素包含ID1,第1个元素包含value1;第2个元素包含ID2;第3个元素包含value2。
因此,您可以使用for循环并将计数器递增2。
发布于 2011-11-29 17:42:25
做到这一点的最好方法是:
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>";
}
}https://stackoverflow.com/questions/8273746
复制相似问题