Skip to content

Instantly share code, notes, and snippets.

@RickStrahl
Last active November 27, 2023 13:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RickStrahl/793ea2c912d49f1f026ecbd15791098e to your computer and use it in GitHub Desktop.
Save RickStrahl/793ea2c912d49f1f026ecbd15791098e to your computer and use it in GitHub Desktop.
void Main()
{
var doc = new XmlDocument();
doc.LoadXml("<d><t>This is &amp; 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 &amp; a "test" and a 'tested' test</t><d2 d2p1:note="" xmlns:d2p1="this &amp; that &lt;doc&gt; and &quot;test&quot; and 'tested'">this &amp; that &lt;doc&gt; and "test" and 'tested'</d2></d>
doc.OuterXml.Dump();
}
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("&lt;");
break;
case '>':
sb.Append("&gt;");
break;
// case '\"':
// sb.Append("&quot;");
// break;
case '&':
sb.Append("&amp;");
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