shololauy
shololauy

Reputation: 13

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>

Upvotes: 0

Views: 165

Answers (1)

Alexander Petrov
Alexander Petrov

Reputation: 14231

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

Upvotes: 1

Related Questions