0

I'd like to use XDocument (or any other similar library) to generate some XHTML. Unfortunately, the nested tags are ending up on the same level, not within one another. Can someone help me with this simple issue?

What I have so far:

var html = new XDocument(
  new XElement("div", new XAttribute("class", "MyTable"),
    new XElement("table",
      new XElement("thead"),
        new XElement("tr"),
          new XElement("th", "Test"))));

This results in the following:

<div class="MyTable">
  <table>
    <thead />
    <tr />
    <th>Test</th>
  </table>
</div>

Here's the layout I would like to achieve:

<div class="MyTable">
  <table>
    <thead>
      <tr>
        <th>Test</th>
      </tr>
    </thead>
  </table>
</div>

1 Answer 1

1
new XDocument(
  new XElement("div", new XAttribute("class", "MyTable"),
    new XElement("table",
      new XElement("thead",
        new XElement("tr",
          new XElement("th", "Test"))))))

Look at your closey brackets on thead and tr. They shouldn't be closed unless you want the element closed. I simply moved them to the end.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I knew I was doing something silly. I just couldn't find it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.