无法在C#中隐式转换类型'string'为'System.Web.HtmlString'?

时间:2021-12-29 16:31:19

I get the error "Cannot implicitly convert type 'string' to 'System.Web.HtmlString' when I attempt to assign a value to a variable of type htmlstring, the value is being read from an xml file (code snippet below)

当我尝试为htmlstring类型的变量赋值时,我得到错误“无法将类型'字符串'隐式转换为'System.Web.HtmlString',该值正在从xml文件中读取(下面的代码片段)

The convert method does not have a built in conversion from string to htmlstring. There is a method ToHtmlString but not sure how to use it in this situation as it is not available with a string object. Your suggestions please.

convert方法没有从string到htmlstring的内置转换。有一个方法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 个解决方案

#1


33  

can you not do content =new HtmlString(c.Value); ?

你不能做content = new HtmlString(c.Value); ?

#1


33  

can you not do content =new HtmlString(c.Value); ?

你不能做content = new HtmlString(c.Value); ?