Class StringExtensions
- Namespace
- Cuemon.Extensions.Xml
- Assembly
- Cuemon.Extensions.Xml.dll
Extension methods for the string class.
public static class StringExtensions
- Inheritance
-
StringExtensions
Examples
StringExtensions in the Xml namespace provides XML escaping, unescaping, element name sanitization, and control-character removal for text content. This example starts with "Use & < > \" ' in XML" and calls EscapeXml to produce "Use & < > " ' in XML", then round-trips back with UnescapeXml. It also demonstrates SanitizeXmlElementName on "1st Element Name!" to replace leading digits and punctuation with underscores, and SanitizeXmlElementText to remove control characters except \t, \n, \r, with optional CDATA section protection that removes "]]>" sequences. Console output displays each transformation result.
using System;
using Cuemon.Extensions.Xml;
namespace MyApp.Xml
{
public class StringExtensionsExample
{
public void Demonstrate()
{
// Escape XML special characters
var unsafeText = "Use & < > \" ' in XML";
var escaped = unsafeText.EscapeXml();
Console.WriteLine(escaped); // "Use & < > " ' in XML"
// Unescape XML back to original text
var unescaped = escaped.UnescapeXml();
Console.WriteLine(unescaped); // "Use & < > \" ' in XML"
// Sanitize a string for use as an XML element name
var invalidName = "1st Element Name!";
var sanitizedName = invalidName.SanitizeXmlElementName();
Console.WriteLine(sanitizedName); // "_st_Element_Name_"
// Sanitize XML element text (remove control characters except \t, \n, \r)
var invalidText = "Valid text \x00 with \x01 control chars";
var sanitizedText = invalidText.SanitizeXmlElementText();
Console.WriteLine(sanitizedText); // "Valid text with control chars"
// Sanitize XML element text with CDATA section rules (discourages "]]>" sequence)
var cdataText = "text with ]]> inside";
var sanitizedCdata = cdataText.SanitizeXmlElementText(cdataSection: true);
Console.WriteLine(sanitizedCdata); // "text with ]] inside"
}}
}
Methods
EscapeXml(string)
Escapes the given XML value.
public static string EscapeXml(this string value)
Parameters
Returns
- string
The input
valuewith an escaped equivalent.
Exceptions
- ArgumentNullException
valuecannot be null.
SanitizeXmlElementName(string)
Sanitizes the value for any invalid characters.
public static string SanitizeXmlElementName(this string value)
Parameters
Returns
Remarks
Sanitation rules are as follows:
1. Names can contain letters, numbers, and these 4 characters: _ | : | . | -
2. Names cannot start with a number or punctuation character
3. Names cannot contain spaces
Exceptions
- ArgumentNullException
valuecannot be null.
SanitizeXmlElementText(string, bool)
Sanitizes the value for any invalid characters.
public static string SanitizeXmlElementText(this string value, bool cdataSection = false)
Parameters
valuestringThe string to extend.
cdataSectionboolif set to
truesupplemental CDATA-section rules is applied tovalue.
Returns
Remarks
Sanitation rules are as follows:
1. The value cannot contain characters less or equal to a Unicode value of U+0019 (except U+0009, U+0010, U+0013)
2. The value cannot contain the string "]]<" if cdataSection is true.
UnescapeXml(string)
Unescapes the given XML value.
public static string UnescapeXml(this string value)
Parameters
Returns
- string
The input
valuewith an unescaped equivalent.
Exceptions
- ArgumentNullException
valuecannot be null.