Different ways how to escape an XML string in C# (zz)
2012-08-01 16:59
483 查看
Different ways how to escape an XML string in C#
//z 2012-08-01 17:02:15 is2120@csdn.T297755689 [T22,L756,R3,V348]XML encoding is necessary if you have to save XML text in an XML document. If you don't escape special chars the XML to insert will become a part of the original XML DOM and not a value of a node.
Escaping the XML means basically replacing 5 chars with new values.
These replacements are:
< | -> | < |
> | -> | > |
" | -> | " |
' | -> | ' |
& | -> | & |
1. string.Replace() 5 times
This is ugly but it works. Note that Replace("&", "&") has to be the first replace so we don't replace other already escaped &.
string xml = "<node>it's my \"node\" & i like it<node>"; encodedXml = xml.Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace("\"", """).Replace("'", "'"); // RESULT: <node>it's my "node" & i like it<node>
2. System.Web.HttpUtility.HtmlEncode()
Used for encoding HTML, but HTML is a form of XML so we can use that too. Mostly used in ASP.NET apps. Note that HtmlEncode does NOT encode apostrophes ( ' ).
string xml = "<node>it's my \"node\" & i like it<node>"; string encodedXml = HttpUtility.HtmlEncode(xml); // RESULT: <node>it's my "node" & i like it<node>
3. System.Security.SecurityElement.Escape()
In Windows Forms or Console apps I use this method. If nothing else it saves me including the System.Web reference in my projects and it encodes all 5 chars.
string xml = "<node>it's my \"node\" & i like it<node>"; string encodedXml = System.Security.SecurityElement.Escape(xml); // RESULT: <node>it's my "node" & i like it<node>
4. System.Xml.XmlTextWriter
Using XmlTextWriter you don't have to worry about escaping anything since it escapes the chars where needed. For example in the attributes it doesn't escape apostrophes, while in node values it doesn't escape apostrophes and qoutes.
string xml = "<node>it's my \"node\" & i like it<node>"; using (XmlTextWriter xtw = new XmlTextWriter(@"c:\xmlTest.xml", Encoding.Unicode)) { xtw.WriteStartElement("xmlEncodeTest"); xtw.WriteAttributeString("testAttribute", xml); xtw.WriteString(xml); xtw.WriteEndElement(); } // RESULT: /* <xmlEncodeTest testAttribute="<node>it's my "node" & i like it<node>"> <node>it's my "node" & i like it<node> </xmlEncodeTest> */
Each of the four ways is different, so use each one where you fell appropriate. You can't go wrong with SecurityElement though. :)
//z 2012-08-01 17:02:15 is2120@csdn.T297755689 [T22,L756,R3,V348]
http://weblogs.sqlteam.com/mladenp/archive/2008/10/21/Different-ways-how-to-escape-an-XML-string-in-C.aspx
相关文章推荐
- Different ways how to escape an XML string in C#
- How to convert XML to JSON in ASP.NET C#
- How to get URL and QueryString value in an ASP.NET page
- Divide and Conquer (1) -- Kth Largest Element in an Array,Different Ways to Add Parentheses
- csharp:Learn how to post JSON string to generic Handler using jQuery in ASP.Net C#.
- How To Use a DataReader Against an Oracle Stored Procedure in Visual C# .NET
- How to convert an IPv4 address into a integer in C#?
- How To Generate An XML File As A Target Datastore Using ODI In An Integration Interface ? [ID 454268
- 如何把string解析为int?[C#] How to Parse a string to an int? [C#]
- How to define an array in c#.
- How to convert XML to JSON in ASP.NET C#
- Different ways to create an object in Java
- How to sum values in an array with different hash
- WebSphere Portal XMLAccess: discover different ways to refer resources in XML script
- How Do I Convert an Integer to a String in Excel VBA
- How do I convert an enum to a list in C#?
- How to create an XTR file from XML in Delphi XE4 using XML Mapper?(delphi中如何通过xmlmapper创建xtr文件)
- How do I convert an enum to a list in C#?
- Serialization from an Object to XML Document in C#
- How can I convert String to Int in C#?