首页 > 解决方案 > 如何在c#中转义字符串中的字符

问题描述

我有这个字符串,并得到一个错误

字符串文本 =

<root><Info id="inseID">17</Info><note><123comments></note></root>

我想把它转换成

<root><Info id="inseID">17</Info><note>&lt;123comments&gt;</note></root>

标签: c#xmlstring

解决方案


使用 linq 到 xml。

var text = "<root><Info id=\"inseID\">17</Info><note></note></root>";
var xml = XElement.Parse(text);
xml.Element("note").Add("<123comments>");
//string result = xml.ToString(); // will be escaped

推荐阅读