2016-08-18 58 views
0

我正在使用C#。
我有以下的html文本: < strong>正在測試</strong>; (沒有開始標記之前的空格,結束標記)。從html標記到xml屬性

我想插入這個HTML文本如下我的HTML元素的屬性:

<MyElement myAttribute=\"&lt;strong&gt;Testing&lt;/strong&gt;\"/> 

如何從HTML格式「轉換」成XML格式的屬性?

回答

0

這是你想要的嗎?上述

new XDocument(
    new XElement("MyElement", 
    new XAttribute("myAttribute", "<strong>Testing</strong>"))).ToString() 

該代碼將生成的字符串:

<MyElement myAttribute="&lt;strong&gt;Testing&lt;/strong&gt;" /> 

的屬性的值是「自動地」通過XAttribute對象逃脫。