我正在使用C#。
我有以下的html文本: < strong>正在測試</strong>; (沒有開始標記之前的空格,結束標記)。從html標記到xml屬性
我想插入這個HTML文本如下我的HTML元素的屬性:
<MyElement myAttribute=\"<strong>Testing</strong>\"/>
如何從HTML格式「轉換」成XML格式的屬性?
我正在使用C#。
我有以下的html文本: < strong>正在測試</strong>; (沒有開始標記之前的空格,結束標記)。從html標記到xml屬性
我想插入這個HTML文本如下我的HTML元素的屬性:
<MyElement myAttribute=\"<strong>Testing</strong>\"/>
如何從HTML格式「轉換」成XML格式的屬性?
您可以簡單地使用https://msdn.microsoft.com/en-us/library/73z22y6h(v=vs.110).aspx
即
string encodeValue = HttpUtility.HtmlEncode("<strong>Testing</strong>");
然後你就可以用encodeValue
插入到你的XML屬性。
這是你想要的嗎?上述
new XDocument(
new XElement("MyElement",
new XAttribute("myAttribute", "<strong>Testing</strong>"))).ToString()
該代碼將生成的字符串:
<MyElement myAttribute="<strong>Testing</strong>" />
的屬性的值是「自動地」通過XAttribute對象逃脫。