首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从WebServices检索到的格式化LookUp值

从WebServices检索到的格式化LookUp值
EN

Stack Overflow用户
提问于 2011-11-26 04:06:16
回答 3查看 2.2K关注 0票数 0

我正在处理列表WebService和检索XML数据,我需要格式化它在一个良好和干净的格式,以便在前端显示。我正在获取如下格式的loopup的值

代码语言:javascript
复制
12;#Infor ERP Baan;#15;#Infor ERP LN;#31;#Infor PM;#32;#Infor SCM

我需要将其显示为项目符号列表,并且需要用";“分隔这些值,这样我就可以放入for循环并添加<li>,如下所示

代码语言:javascript
复制
 Infor ERP Baan;Infor ERP LN;Infor PM;Infor SCM
EN

回答 3

Stack Overflow用户

发布于 2013-03-09 02:31:02

在从SharePoint返回查找数据时,我使用了以下函数和正则表达式来拆分数据。

代码语言:javascript
复制
    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部分(看起来不太可能),则可以处理该分隔符。

它还具有以下优点

  1. 查找id中的值
  2. 从字典中获取id数组如果值存在于字典中,则
  3. 从字典中获取值的数组如果id存在于字典中,则

希望这能有所帮助。

票数 2
EN

Stack Overflow用户

发布于 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。

票数 1
EN

Stack Overflow用户

发布于 2011-11-29 17:42:25

做到这一点的最好方法是:

代码语言:javascript
复制
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>";
                            }
                        }
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8273746

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档