{"id":787,"date":"2012-03-08T09:01:46","date_gmt":"2012-03-08T07:01:46","guid":{"rendered":"http:\/\/hen.co.za\/blog\/?p=787"},"modified":"2012-03-09T07:19:23","modified_gmt":"2012-03-09T05:19:23","slug":"htmlwriter-1-5","status":"publish","type":"post","link":"https:\/\/hen.co.za\/blog\/2012\/03\/htmlwriter-1-5\/","title":{"rendered":"HTMLWriter 1.5"},"content":{"rendered":"<p>I&#8217;ve updated this little utility library by\u00a0simplifying some method overloads plus added a static method for getting escaped html special characters.<\/p>\n<p>To make the code more maintainable for the special characters (like adding new ones in the future) I&#8217;ve added an enum that use some .Net &#8216;attributes&#8217; so you can see, use and reference everything about these &#8216;things&#8217; in one place. To do that I had to add a special helper class so the enum can carry some additional properties (like the actual strings for the special character and the escaped string). To do this I created a custom attribute class called &#8216;StringValueAttribute&#8217; like this:<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">public class<\/span> StringValueAttribute : <span style=\"color: #0000ff;\">Attribute<\/span><br \/>\n{<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">public string<\/span> StringExpandedValue;<br \/>\n<span style=\"color: #0000ff;\">public string<\/span> StringValue;<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">public<\/span> StringValueAttribute(<span style=\"color: #0000ff;\">string<\/span> value, <span style=\"color: #0000ff;\">string<\/span> stringExpandedValue)<br \/>\n{<\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">this<\/span>.StringValue = value;<br \/>\n<span style=\"color: #0000ff;\">this<\/span>.StringExpandedValue = stringExpandedValue;<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p>}<\/p><\/blockquote>\n<p>To retrieve the associated values from an enum another static helper class is needed:\u00a0StringValueAttributeUtil<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">internal static class<\/span> StringValueAttributeUtil<br \/>\n{<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">public static string<\/span> GetStringValue(<span style=\"color: #0000ff;\">Enum<\/span> value)<br \/>\n{<\/p>\n<p style=\"padding-left: 60px;\">\/\/ Get the type<br \/>\n<span style=\"color: #0000ff;\">Type<\/span> type = value.GetType();<\/p>\n<p style=\"padding-left: 60px;\">\/\/ Get fieldinfo for this type<br \/>\nSystem.Reflection.<span style=\"color: #0000ff;\">FieldInfo<\/span> fieldInfo = type.GetField(value.ToString());<br \/>\n\/\/ Get the stringvalue attributes<br \/>\n<span style=\"color: #0000ff;\">StringValueAttribute<\/span>[] attribs = fieldInfo.GetCustomAttributes(<br \/>\ntypeof(<span style=\"color: #0000ff;\">StringValueAttribute<\/span>), <span style=\"color: #0000ff;\">false<\/span>) as <span style=\"color: #0000ff;\">StringValueAttribute<\/span>[];<br \/>\n\/\/ Return the first if there was a match.<br \/>\n<span style=\"color: #0000ff;\">return<\/span> attribs.Length &gt; 0 ? attribs[0].StringValue : <span style=\"color: #0000ff;\">null<\/span>;<\/p>\n<p style=\"padding-left: 30px;\">}<br \/>\n<span style=\"color: #0000ff;\">public static string<\/span> GetStringExpandedValue(<span style=\"color: #0000ff;\">Enum<\/span> value)<br \/>\n{<\/p>\n<p style=\"padding-left: 60px;\">\/\/ Get the type<br \/>\n<span style=\"color: #0000ff;\">Type<\/span> type = value.GetType();<\/p>\n<p style=\"padding-left: 60px;\">\/\/ Get fieldinfo for this type<br \/>\nSystem.Reflection.<span style=\"color: #0000ff;\">FieldInfo<\/span> fieldInfo = type.GetField(value.ToString());<br \/>\n\/\/ Get the stringvalue attributes<br \/>\n<span style=\"color: #0000ff;\">StringValueAttribute<\/span>[] attribs = fieldInfo.GetCustomAttributes(<br \/>\ntypeof(<span style=\"color: #0000ff;\">StringValueAttribute<\/span>), <span style=\"color: #0000ff;\">false<\/span>) as <span style=\"color: #0000ff;\">StringValueAttribute<\/span>[];<br \/>\n\/\/ Return the first if there was a match.<br \/>\n<span style=\"color: #0000ff;\">return<\/span> attribs.Length &gt; 0 ? attribs[0].StringExpandedValue : <span style=\"color: #0000ff;\">null<\/span>;<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p>}<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>Then to actually make use of this helper class you decorate the enum like this: (showing only part of it&#8230;)<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">public enum<\/span> HTMLSpecialCharacter<br \/>\n{<\/p>\n<p style=\"padding-left: 30px;\">[<span style=\"color: #0000ff;\">StringValue<\/span>(&#8220;&lt;&#8220;, &#8220;&amp;amp;lt;&#8221;)]<br \/>\nLessThanSign,<br \/>\n[<span style=\"color: #0000ff;\">StringValue<\/span>(&#8220;&gt;&#8221;, &#8220;&amp;amp;gt;&#8221;)]<br \/>\nGreaterThanSign,<br \/>\n[<span style=\"color: #0000ff;\">StringValue<\/span>(&#8220;\\&#8221;&#8221;, &#8220;&amp;amp;quot;&#8221;)]<br \/>\nDoubleQuote,<br \/>\n[<span style=\"color: #0000ff;\">StringValue<\/span>(&#8220;&#8216;&#8221;, &#8220;&amp;amp;#39;&#8221;)]<br \/>\nSingleQuote,<\/p>\n<p style=\"padding-left: 30px;\">&#8230;<\/p>\n<p>}<\/p><\/blockquote>\n<p>Happy HTML&#8217;ing \ud83d\ude42<\/p>\n<p>Update: <a title=\"HTMLWriter 1.6\" href=\"http:\/\/htmlwriter.codeplex.com\/releases\/view\/83823\" target=\"_blank\">Version 1.6<\/a> has now been released since&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve updated this little utility library by\u00a0simplifying some method overloads plus added a static method for getting escaped html special characters. To make the code more maintainable for the special characters (like adding new ones in the future) I&#8217;ve added &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/hen.co.za\/blog\/2012\/03\/htmlwriter-1-5\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[7,8,9,303,25,145,304],"class_list":["post-787","post","type-post","status-publish","format-standard","hentry","category-development","category-projects","tag-net","tag-c","tag-code","tag-development","tag-html","tag-htmlwriter","tag-projects"],"_links":{"self":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/787","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/comments?post=787"}],"version-history":[{"count":6,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/787\/revisions"}],"predecessor-version":[{"id":792,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/787\/revisions\/792"}],"wp:attachment":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/media?parent=787"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/categories?post=787"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/tags?post=787"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}