C# 格式查找值,从Web服务检索
我正在使用Lists WebService和XML数据的检索,我需要将其格式化为一种漂亮、干净的格式,以便在前端显示。我得到如下格式的loopup值C# 格式查找值,从Web服务检索,c#,linq,web-services,sharepoint,C#,Linq,Web Services,Sharepoint,我正在使用Lists WebService和XML数据的检索,我需要将其格式化为一种漂亮、干净的格式,以便在前端显示。我得到如下格式的loopup值 12;#Infor ERP Baan;#15;#Infor ERP LN;#31;#Infor PM;#32;#Infor SCM 我需要将它显示为一个项目符号列表,因为我需要将值用“;”分隔,我可以将其放入for循环并添加,类似于 Infor ERP Baan;Infor ERP LN;Infor PM;Infor SCM SharePoi
12;#Infor ERP Baan;#15;#Infor ERP LN;#31;#Infor PM;#32;#Infor SCM
我需要将它显示为一个项目符号列表,因为我需要将值用“;”分隔,我可以将其放入for循环并添加
,类似于
Infor ERP Baan;Infor ERP LN;Infor PM;Infor SCM
SharePoint中的查找字段值包含两条信息—被查找项的ID和引用字段的文本值。使用
将它们组合在一个字符串中#代码>作为分隔符。
这里的值是SPFieldLookupMulti
-字段,此时可以选择多个值。因此,它包含ID1#价值1#ID2#价值2
最简单的解决方法是字符串#
substring(请参阅此响应以了解如何:),然后仅访问结果数组的偶数索引:第0个元素包含ID1,第1个元素包含value1;第二个元素包含ID2;第三个元素包含value2
因此,您可以对
循环使用,并将计数器增加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>";
}
}
ProductsCellData=“12;#Infor ERP Baan;#15;#Infor ERP LN;#31;#Infor PM;#32;#Infor SCM”
字符串[]nProductsCellData=Regex.Replace(ProductsCellData,@“\d”,“;”).Replace(“;#”,”).Replace(“;”,“;”).Split(“;”);
foreach(nProductsCellData中的字符串乘积)
{
如果(产品!=“”)
{
e、 行。单元格[i]。文本+=“”+产品+“ ”;
}
}
从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;
}
静态私有字典GetValues(字符串productsCellData)
{
//正则表达式要将数据拆分为数组,我们需要ExplictCapture
//防止c#捕获病毒#
var regex=new regex(@“(?string[]nProductsCellData=regex.Replace(ProductsCellData,@“\d”,“;”).Replace(;#“,”).Replace(;;”,“;”).Split(“;”);foreach(nProductsCellData中的字符串产品){if(product!=”){e.Row.Cells[i].Text+=“”+product+“ ”;}}}回答得很好。我有这个需要,并且在做了很多关于在哪里做的研究之后(例如,在使用PATINDEX
和REPLACE
的T-SQL中,或者在使用Regex的C#中,我确定了您的方法。正如您所说,它非常灵活。我编写了一个控制台应用程序,在我的项目中测试各种查找字符串实例的代码,效果非常好!谢谢!)!!