Last active
November 27, 2023 13:18
-
-
Save RickStrahl/793ea2c912d49f1f026ecbd15791098e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
var doc = new XmlDocument(); | |
doc.LoadXml("<d><t>This is & a \"test\" and a 'tested' test</t></d>"); | |
doc.OuterXml.Dump(); | |
var node = doc.CreateElement("d2"); | |
node.InnerText = "this & that <doc> and \"test\" and 'tested'"; | |
doc.DocumentElement.AppendChild(node); | |
var attr = doc.CreateAttribute("note","this & that <doc> and \"test\" and 'tested'"); | |
node.Attributes.Append(attr); | |
// <d><t>This is & a "test" and a 'tested' test</t><d2 d2p1:note="" xmlns:d2p1="this & that <doc> and "test" and 'tested'">this & that <doc> and "test" and 'tested'</d2></d> | |
doc.OuterXml.Dump(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
var s = "Characters: < > \" ' &" + ((char) 10); | |
for (int i = 0; i < 5; i++) | |
{ | |
s = s + s; | |
} | |
s.Dump(); | |
var sw = new Stopwatch(); | |
sw.Start(); | |
for (int i = 0; i < 300000; i++) | |
{ | |
XmlStringEx(s); | |
} | |
sw.Stop(); | |
sw.ElapsedMilliseconds.Dump(); | |
("XElement: " + XmlStringEx(s)).Dump(); | |
sw.Reset(); | |
sw.Start(); | |
for (int i = 0; i < 300000; i++) | |
{ | |
XmlString(s); | |
} | |
sw.Stop(); | |
sw.ElapsedMilliseconds.Dump(); | |
("String: " + XmlString(s)).Dump(); | |
sw.Reset(); | |
sw.Start(); | |
for (int i = 0; i < 300000; i++) | |
{ | |
XmlStringEscaped(s); | |
} | |
sw.Stop(); | |
sw.ElapsedMilliseconds.Dump(); | |
("Escaped: " + XmlStringEscaped(s)).Dump(); | |
sw.Reset(); | |
sw.Start(); | |
for (int i = 0; i < 300000; i++) | |
{ | |
XmlStringDoc(s); | |
} | |
sw.Stop(); | |
sw.ElapsedMilliseconds.Dump(); | |
("XmlDoc: " + XmlStringDoc(s)).Dump(); | |
} | |
// Define other methods and classes here | |
public static string XmlString(string text, bool isAttribute = false) | |
{ | |
if (string.IsNullOrEmpty(text)) | |
return text; | |
if (!isAttribute) | |
return new XElement("t", text).LastNode.ToString(); | |
return new XAttribute("__n",text) | |
.ToString().Substring(5).TrimEnd('\"'); | |
} | |
private static XmlDocument _xmlDoc; | |
public string XmlStringDoc(string text) | |
{ | |
_xmlDoc = _xmlDoc ?? new XmlDocument(); | |
var el = _xmlDoc.CreateElement("t"); | |
el.InnerText = text; | |
return el.InnerXml; | |
} | |
public static string XmlString(string text) | |
{ | |
var sb = new StringBuilder(text.Length); | |
foreach(var chr in text) | |
{ | |
switch(chr) | |
{ | |
case '<': | |
sb.Append("<"); | |
break; | |
case '>': | |
sb.Append(">"); | |
break; | |
// case '\"': | |
// sb.Append("""); | |
// break; | |
case '&': | |
sb.Append("&"); | |
break; | |
// legal control characters | |
case '\n': | |
sb.Append("\n"); | |
break; | |
case '\r': | |
sb.Append("\r"); | |
break; | |
case '\t': | |
sb.Append("\t"); | |
break; | |
default: | |
if (chr < 32) | |
throw new InvalidOperationException("Invalid character in Xml String. Chr " + Convert.ToInt16(chr) + " is illegal."); | |
else | |
sb.Append(chr); | |
break; | |
} | |
} | |
return sb.ToString(); | |
} | |
public static string XmlStringEscaped(string input) | |
{ | |
return System.Security.SecurityElement.Escape(input); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment