Wednesday, November 19, 2008

LINQ : Simple LINQ to XML

It's really simple to construct XML documents with LINQ. This simple construct (inside LINQPad):


XDocument simpleDoc =
  new XDocument(
    new XElement("TopLevel",
      new XElement("SecondLevel",
        new XElement("a", "b",
        new XAttribute("class", "level"))
      )
    )
  );

simpleDoc.Dump();


produces:


<TopLevel>
  <SecondLevel>
    <a class="level">b</a>
  </SecondLevel>
</TopLevel>


Enjoy!

No comments: