C# で 'string' 型を 'System.Web.HtmlString' に暗黙的に変換できませんか? 質問する

C# で 'string' 型を 'System.Web.HtmlString' に暗黙的に変換できませんか? 質問する

htmlstring 型の変数に値を割り当てようとすると、「'string' 型を 'System.Web.HtmlString' に暗黙的に変換できません。値は xml ファイルから読み取られます (以下のコード スニペット)」というエラーが表示されます。

convert メソッドには、文字列から HTML 文字列への変換が組み込まれていません。ToHtmlString メソッドはありますが、文字列オブジェクトでは使用できないため、この状況でどのように使用すればよいかわかりません。ご提案をお願いします。

public class Xclass
{
    public HtmlString content { get; set; }
    public string id { get; set; }    
}

Xclass x = (from c in xdoc.Descendants("div") select new Xclass()
{
    content = c.Value, //c.value is the html content of div, content is a property of   type HtmlString 
    id = c.id
});

ベストアンサー1

できないんですかcontent =new HtmlString(c.Value);

おすすめ記事