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> 
Add Comment
1 Answer(s)

Use linq to 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 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.