2012-02-20 7 views
2

私はこのリストの各値にhtmlEncodeを行う場合は、このHtmlEncode一覧<string>値

public List<string> Cell { get; set; } 

のように一覧に保存されたセルの値を持っています。 誰もがこれで私を助けることができますか?

+0

あなたがリストまたはリストセルに追加されているコードがありますか..?適切なコードを表示してください。 – MethodMan

答えて

0

新しい拡張メソッドを使用してトリック

public static class MyCustomListMethod 
    { 
     public static void Add(this IList<string> list, string item,bool htmlEndode){ 
      list.Add(System.Web.HttpUtility.HtmlEncode(item)); 
     } 
    } 

、その後

public static void Main() 
     { 
      string e = "<p>Test Text<p/>"; 
      List<string> mylist = new List<string>(); 
      mylist.Add(e,true); 

     } 

がすでに代わりの変換のエンコードされたリストに項目を追加することができるようになりますん拡張メソッドを作成することができますすべて

2
Cell = Cell.Select(s => System.Web.HttpUtility.HtmlEncode(s)).ToList(); 

または short:

Cell = Cell.Select(System.Web.HttpUtility.HtmlEncode).ToList(); 
2

あなたはConvertAll<T>拡張メソッドを使用できます

Cell = Cell.ConvertAll<string>(s => System.Web.HttpUtility.HtmlEncode(s)); 

か短いの:

Cell = Cell.ConvertAll<string>(System.Web.HttpUtility.HtmlEncode); 
+0

あなたのソリューションは私のものより優れていると思います。私は 'ConvertAll'について知らなかった。何について: 'Cell = Cell.ConvertAll(System.Web.HttpUtility.HtmlEncode);'。私はそれをupvoteだろう。 –

+0

@PhilipFourie:ありがとう、あなたの短いバージョンを追加しました。 –