<p>I'm a C# developer who's fumbling in the first VB code he's written since VB6, so if I am asking a rather obvious question, please forgive me.</p>

<p>I decided to experiment with XML Literals to generate some XML code for me, instead of using XMLDocument</p>

<p>I have 2 questions, the second regarding a workaround due to my inability to figure out the first.</p>

<p>1: Ideal solution</p>

<p>I have a Dictionary of ElementName, ElementValue whose KeyValue pairs I was looping over in the hope of generating the values dynamically, but the following syntax dies a horrible death</p>

<pre><code>Dim xConnections As XElement        
For Each connection As Connection In connections.AsList
    For Each kvp As KeyValuePair(Of String, String) In connection.DecompiledElements
        xConnections = &lt;Connections&gt; &lt;&lt;%= kvp.Key %&gt;&gt;&lt;%= kvp.Value %&gt;&lt;\&lt;%=kvp.Key %&gt;&gt;  &lt;/Connections&gt;
    Next
Next
</code></pre>

<p>I have vague memories of the T4 syntax (the &lt;%=%> syntax) being able to handle more complex operations (rather than direct assignment to the &lt;%= ) and a 'Response.Write' like object to write output to, but I can't remember the details.</p>

<p>2: Cludgy workaround</p>

<p>Instead I thought of building a StringBuilder object and assigning its .ToString to the XElement, but that also failed with a conversion error.</p>

<p>I would prefer to continue using my key value pair concept in example one above, as I feel cludging together a string as in example 2 above is rather nasty, and I really should go back to using XMLDocument if instead.</p>

<p>Any thoughts or assistance greatly appreciated</p>

[{'answer_id': 51025, 'author': 'DaveK', 'author_id': 4244, 'author_profile': 'https://Stackoverflow.com/users/4244', 'pm_score': 1, 'selected': True, 'text': "<p>If I understand correctly what you are trying to do, you can use the StringBuilder. Use the StringBuilder.Append method and append the XmlElement 'OuterXml' property.</p>\n\n<p>For example:</p>\n\n<p>sb.Append(xmlElement.OuterXml)</p>\n"}, {'answer_id': 51312, 'author': 'Frank Krueger', 'author_id': 338, 'author_profile': 'https://Stackoverflow.com/users/338', 'pm_score': -1, 'selected': False, 'text': '<p>We would all be remiss not to mention that dynamic XML element names are generally a bad idea. The whole point of XML is to create a store a data structure in a form that is readily:</p>\n\n<ol>\n<li>Verifiable</li>\n<li>Extendable</li>\n</ol>\n\n<p>Dynamic element names fail that first condition. Why not simply use a standard XML format for storing key/value pairs like <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/PropertyLists/Articles/XMLPListsConcept.html" rel="nofollow noreferrer">plists</a>?</p>\n\n<pre><code>&lt;dict&gt;\n    &lt;key&gt;Author&lt;/key&gt;\n    &lt;string&gt;William Shakespeare&lt;/string&gt;\n    &lt;key&gt;Title&lt;/key&gt;\n    &lt;string&gt;Romeo et&lt;/string&gt;\n    &lt;key&gt;ISBN&lt;/key&gt;\n    &lt;string&gt;?????&lt;/string&gt;\n&lt;/dict&gt;\n</code></pre>\n'}, {'answer_id': 796938, 'author': 'CoderDennis', 'author_id': 69527, 'author_profile': 'https://Stackoverflow.com/users/69527', 'pm_score': 3, 'selected': False, 'text': '<p>VB.NET XML Literals are very powerful, but most often adding some LINQ to them makes them truly awesome. This code should do exactly what you\'re trying to do.</p>\n\n<pre><code>Dim Elements = New Dictionary(Of String, String)\nElements.Add("Key1", "Value1")\nElements.Add("Key2", "Value2")\nElements.Add("Key3", "Value3")\n\nDim xConnections = &lt;Connections&gt;\n                       &lt;%= From elem In Elements _\n                           Select &lt;&lt;%= elem.Key %&gt;&gt;&lt;%= elem.Value %&gt;&lt;/&gt; %&gt;\n                   &lt;/Connections&gt;\n</code></pre>\n\n<p>The empty closing tag <code>&lt;/&gt;</code> is all that is needed for the vb compiler to properly construct an xml element whose name is generated from a value within a <code>&lt;%= %&gt;</code> block.</p>\n\n<p>Calling xConnections.ToString renders the following:</p>\n\n<pre><code>&lt;Connections&gt;\n    &lt;Key1&gt;Value1&lt;/Key1&gt;\n    &lt;Key2&gt;Value2&lt;/Key2&gt;\n    &lt;Key3&gt;Value3&lt;/Key3&gt;\n&lt;/Connections&gt;\n</code></pre>\n'}, {'answer_id': 1028596, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>To answer this more completely...</p>\n\n<p>When injecting Strings into an XML Literal, it will not work properly unless you use XElement.Parse when injecting an XElement (this is because special characters are escaped)</p>\n\n<p>So your ideal solution is more like this:</p>\n\n<pre><code>Dim conns = connections.AsList()\nIf conns IsNot Nothing AndAlso conns.length &gt; 0 Then\n    Dim index = 0\n    Dim xConnections = _\n        &lt;Connections&gt;\n            &lt;%= From kvp As KeyValuePair(Of String, String) In conns (System.Threading.Interlocked.Increment(index)).DecompiledElements() _\n            Select XElement.Parse("&lt;" &amp; &lt;%= kvp.Key %&gt; &amp; "&gt;" &amp; &lt;%= kvp.Value %&gt; &amp; "&lt;/" &amp; &lt;%= kvp.Key %&gt; &amp; "&gt;") _\n             %&gt;\n        &lt;/Connections&gt;\n    Return xConnections.ToString()\nEnd If\n</code></pre>\n\n<p>ToString will return the OuterXML Properly as a String (Value will not...)\nof course, just drop the ToString() if you want to return an XElement of </p>\n\n<p>Since I don\'t know what AsList() does, nor do I know what DecompiledElements do, set your error trapping accordingly.  There are other ways to do the loops as well, this is just one solution.</p>\n'}]
['https://Stackoverflow.com/questions/50995', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5302/']