Thursday, February 25, 2010

Create XML by using LINQ

Is it possible to create an XML from a single statement? Yes, it is possible through LINQ. I think this is the easiest way to create an XML string. Is there any other method much easier than this?

Here is the code.

var xml = new XElement("Products",
     new XElement("Product",
            new XElement("ProductId", "1001"),
                   new XElement("ProductDesc","Product1"),
                           new XElement("UOM", "20")));

Here is the output.

<Products>
          <Product>
                  <ProductId>1001</ProductId>
                 <ProductDesc>Product1</ProductDesc>
                 <UOM>20</UOM>
          </Product>
</Products>

0 comments:

Post a Comment