2011-11-25 37 views
0

我正在使用Lists WebService並檢索XML數據,我需要將其格式化爲一種非常乾淨的格式,以便在前端顯示。我得到loopup值如下格式格式LookUp值,從WebServices中檢索

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 

回答

1

在SharePoint查閱字段值包含兩個部分信息 - 要查找的項目的ID和引用字段的文本值。這些使用;#作爲分隔符組合在一個字符串中。 您在這裏得到的值是SPFieldLookupMulti - 您可以在此處選擇多個值。因此,它包含ID1;#值1;#ID2;#值2 ...

最簡單的解決方案是String.Split增長;#子(見本應找出來,如何:https://stackoverflow.com/q/1126933/239599)將只能訪問的偶數索引結果數組:第0個元素包含ID1,第1個元素包含value1;第二個元素包含ID2;第三個元素包含值2。

所以,你可以使用一個for循環,並增加由2

+0

string [] nProductsCellData = Regex.Replace(ProductsCellData,@「\ d」,「;」)。Replace(「;#」,「」).Replace(「;;」,「;」)。Split ';'); (!產物= 「」) 的foreach(在nProductsCellData串產品) { 如果 { e.Row.Cells [I]。文本+ = 「

  • 」 +產物+ 「
  • 」; } } – Vishal

    -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>"; 
              } 
             } 
    
    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; 
        } 
    

    這具有處理分隔符的優點;#如果它出現(不太可能,因爲它似乎)在值部分。

    它還具有以下優點從ID

    1. 查找值
    2. 從字典
    3. 獲取編號的數組從字典中獲取值數組
    4. 如果存在檢查的值字典
    5. 檢查字典中是否存在編號

    希望這有助於。

    +0

    優秀的答案。我有這方面的需求,並經過大量的研究(如在使用'PATINDEX'和'REPLACE'的T-SQL或使用正則表達式的C#中進行了大量研究後,我決定採用您的方法。就像您說的那樣,它非常靈活。我寫了一個控制檯應用程序來測試你的代碼在我的項目中的各種查找字符串實例,這很好!謝謝! – Shiva