简体   繁体   English

如何在 c# 中转义字符串中的字符

[英]How to escape characters in string in c#

I have this string,and getting an error我有这个字符串,并得到一个错误

string text =字符串文本 =

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

I wanted to convert it to我想把它转换成

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

Use linq to xml.使用 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM