Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Linq's <code>Single</code> method returns the first and only element of a sequence - if there's more than one element it throws the exception you're seeing. As your table row contains two table cells, its <code>o.Elements()</code> method returns an enumerable with two members, and using <code>Single</code> makes it fall over.</p> <p>Instead of using:</p> <pre><code>control.Controls.Add(constructHtmlStructure(o.Elements().Single())); </code></pre> <p>...use:</p> <pre><code>foreach(var element in o.Elements()) { control.Controls.Add(constructHtmlStructure(element)); } </code></pre> <p>That way you iterate over each of the table row's cells, and add each one to the control.</p> <p><strong>Edit</strong></p> <p>There's another change you need to make. Your dictionary contains exactly one <code>HtmlTableRow</code>, and exactly one <code>HtmlTableRow</code>, both already created by the time you get into <code>constructHtmlStructure</code>. Instead of having pre-existing objects (and therefore only one instance of each available) you should have your dictionary create a new instance of each type on demand.</p> <p>So instead of this (adjusted for spacing):</p> <pre><code>var controlConstructor = new Dictionary&lt;string, HtmlContainerControl&gt; { { "tr", new HtmlTableRow() }, { "td", new HtmlTableCell() } }; ... var control = controlConstructor[o.Name.ToString()]; </code></pre> <p>...try this:</p> <pre><code>var controlConstructor = new Dictionary&lt;string, Func&lt;XElement, HtmlControl&gt;&gt; { { "tr", o =&gt; new HtmlTableRow() }, { "td", o =&gt; new HtmlTableCell() } }; ... var control = controlConstructor[o.Name.ToString()].Invoke(o); </code></pre> <p>You'll also need a function to create an input from an <code>XElement</code>, something like this:</p> <pre><code>private static HtmlControl CreateInputFromElement(XElement element) { // Create an appropriate HtmlInputControl... } </code></pre> <p>...which you can add to your dictionary like this:</p> <pre><code>var controlConstructor = new Dictionary&lt;string, Func&lt;XElement, HtmlControl&gt;&gt; { { "tr", o =&gt; new HtmlTableRow() }, { "td", o =&gt; new HtmlTableCell() }, { "input", CreateInputFromElement } }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload