String escape/unescape into XML

时间:2023-12-06 09:36:14

Is there any C# function which could be used to escape and un-escape a string, which could be used to fill in the content of an XML element?

And how to unescape?

 

【解决方案】

public
static
string
XmlEscape(string unescaped)

{

XmlDocument doc = new
XmlDocument();

XmlNode node = doc.CreateElement("root");

node.InnerText = unescaped;

return node.InnerXml;

}

 

public
static
string
XmlUnescape(string escaped)

{

XmlDocument doc = new
XmlDocument();

XmlNode node = doc.CreateElement("root");

node.InnerXml = escaped;

return node.InnerText;

}