{"id":231,"date":"2011-02-18T13:55:24","date_gmt":"2011-02-18T13:55:24","guid":{"rendered":"http:\/\/www.ericwhite.com\/home2\/bm8qcmjy\/public_html\/blog\/?p=231"},"modified":"2011-02-24T22:56:30","modified_gmt":"2011-02-24T22:56:30","slug":"refinement-generating-c-code-from-an-xml-tree-using-virtual-extension-methods","status":"publish","type":"post","link":"https:\/\/www.ericwhite.com\/blog\/2011\/02\/18\/refinement-generating-c-code-from-an-xml-tree-using-virtual-extension-methods\/","title":{"rendered":"Refinement: Generating C# code from an XML Tree using Virtual Extension Methods"},"content":{"rendered":"<p>I\u2019ve made great progress on this example to generate WordprocessingML documents from a template that is driven from C# that is written inside of content controls.\u00a0 It took me longer than I thought it would, but I\u2019ve been mostly on vacation this past week, so had to fit in coding around lots of other activities.\u00a0 Also, there is <a href=\"http:\/\/en.wikipedia.org\/wiki\/Hofstadter's_law\">Hofstadter&#8217;s law<\/a> to take into account.<\/p>\n<p>This post is the seventh in a series of blog posts on generating Open XML documents. Here is the complete list: <a href=\"https:\/\/www.ericwhite.com\/blog\/map\/generating-open-xml-wordprocessingml-documents-blog-post-series\/\">Generating Open XML WordprocessingML Documents Blog Post Series<\/a><\/p>\n<p>Along the way, I determined that I needed to refine the <a href=\"https:\/\/www.ericwhite.com\/blog\/2011\/02\/07\/generating-c-code-from-an-xml-tree-using-virtual-extension-methods\/\">approach for generating C# code from an XML tree<\/a>.\u00a0 There were two refinements:<\/p>\n<ul>\n<li>I needed to use the @\u201d\u201d syntax for strings (called <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa691090(v=vs.71).aspx\">verbatim string literals<\/a>).\u00a0 This handles cases such as insignificant white space that contains new lines.\u00a0 My document generation example doesn\u2019t need this functionality, but I dislike incomplete solutions.<\/li>\n<li>I added an approach for injecting arbitrary code into the code that creates the XML tree.\u00a0 I need to inject the code that the developer types into content controls into the code that creates the XML tree.\u00a0 My approach is that if the transform encounters an element with the fully qualified name of {http:\/\/www.ericwhite.com\/xmlcodegeneration}Literal then the text contents of that element are directly injected into the code to create the XML tree.<\/li>\n<\/ul>\n<p>The following code shows what I mean.\u00a0 The code creates an XML tree that contains an element with the special namespace\/name:<\/p>\n<p><code>XNamespace&nbsp;ewx&nbsp;=&nbsp;\"http:\/\/www.ericwhite.com\/xmlcodegeneration\";<br \/>\nXDocument&nbsp;root&nbsp;=&nbsp;new&nbsp;XDocument(<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;XElement(\"Root\",<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;XElement(\"Child\",&nbsp;123),<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;XElement(ewx&nbsp;+&nbsp;\"Literal\",<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@\"new&nbsp;XElement(\"\"Data\"\",&nbsp;123),&nbsp;&nbsp;\/\/&nbsp;injected&nbsp;code\"&nbsp;+&nbsp;Environment.NewLine),<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;XElement(\"Child\",&nbsp;345)));<br \/>\nConsole.WriteLine(LtxToCode.XDocumentToCode(root));<br \/>\n<\/code><\/p>\n<p>When you run this code, it generates the following code that contains directly injected code:<\/p>\n<p><code>new&nbsp;XDocument(<br \/>\n&nbsp;&nbsp;new&nbsp;XElement(\"Root\",<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;XElement(\"Child\",<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;XText(@\"123\")<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;),<br \/>\nnew&nbsp;XElement(\"Data\",&nbsp;123),&nbsp;&nbsp;\/\/&nbsp;injected&nbsp;code<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;XElement(\"Child\",<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;XText(@\"345\")<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;)<br \/>\n&nbsp;&nbsp;)<br \/>\n)<br \/>\n<\/code><\/p>\n<p>To avoid having two versions of the code posted on my blog, I&#8217;ve altered the <a href=\"https:\/\/www.ericwhite.com\/blog\/2011\/02\/07\/generating-c-code-from-an-xml-tree-using-virtual-extension-methods\/\">previous post<\/a> to contain the corrected code.  In the next post, I\u2019ll discuss pros and cons of <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb126445.aspx\">text templates<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019ve made great progress on this example to generate WordprocessingML documents from a template that is driven from C# that is written inside of content controls.\u00a0 It took me longer than I thought it would, but I\u2019ve been mostly on vacation this past week, so had to fit in coding around lots of other activities.\u00a0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"_s2mail":"","footnotes":""},"categories":[7,3,5],"tags":[],"class_list":["post-231","post","type-post","status-publish","format-standard","hentry","category-document-generation-series","category-open-xml","category-wordprocessingml"],"_links":{"self":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/posts\/231","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/comments?post=231"}],"version-history":[{"count":10,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/posts\/231\/revisions"}],"predecessor-version":[{"id":253,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/posts\/231\/revisions\/253"}],"wp:attachment":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/media?parent=231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/categories?post=231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/tags?post=231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}