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

标签 c# asp.net-mvc compiler-errors

当我尝试为 htmlstring 类型的变量赋值时,出现错误“无法将类型‘string’隐式转换为‘System.Web.HtmlString’,该值是从 xml 文件中读取的(下面的代码片段) )

convert 方法没有内置的从字符串到 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
});

最佳答案

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

关于c# - 无法在 C# 中将类型 'string' 隐式转换为 'System.Web.HtmlString'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8738096/

相关文章:

c# - Entity Framework SQL 查询执行

c# - 如何为 DevExpress WPF GridControl 行着色?

c# - 总时间 (HH :MM) in RDLC

c# - 在项目之间共享对象和信息

c# - 用LINQ查询基本问题

c# - 使用 SQL 数据库在 Azure 门户上部署 ASP.NET MVC 应用程序

javascript - 无法在 Razor 的部分 View 中使用脚本渲染部分

ios - Swift-音频-按键声音不断出错

compiler-errors - Visual Studio Express 2013 中的 Windows 窗体错误

python - 如何处理 Pytjon 列表列表中具有二进制值的样本的索引超出范围和无效语法?