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?
I am using VSTS 2008 + C# + .Net 3.0.
EDIT 1: I am concatenating simple and short XML file and I do not use serialization, so I need to explicitly escape XML character by hand, for example, I need to put a<b
into <foo></foo>
, so I need escape string a<b
and put it into element foo.
XML escape characters There are only five: " " ' ' < < > > & & Escaping characters depends on where the special character is used. The examples can be validated at the W3C Markup Validation Service.
Colon is a reserved character in XML identifiers, and may be used only for binding XML Namespaces. There is no way to escape it for other purposes.
SecurityElement.Escape(string s)
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With