Geeks With Blogs


INauseous() Shawn Cicoria - Solution Architect, Craftsman and Artisan - INauseous() - Main Blog Here:

Just posted about reading XML via LINQ. Now, this post is about the opposite direction - emitting XML from a collection. The simplicity of LINQ provides a straightforward interaction with XML makes LINQ to XML for me one of the easiest and most natural ways to interact with XML.

The following emits a collection of objects out to an XElement (which can be streamed elsewhere as required.

   35 Customer[] customers = new Customer[]{
   36    new Customer{
   37     firstName="john", lastName="lennon"},
   38    new Customer{
   39       firstName="ringo", lastName="starr"} };
   41 XNamespace ns1 = "UGLY_NAMESPACE";
   43 XElement custs = new XElement("myRoot",
   44 from c in customers
   45  select new XElement(ns1 + "customer",
   46   new XElement(ns1 + "firstName", c.firstName),
   47   new XElement(ns1 + "lastName", c.lastName)));
This creates the following XML
    1 <myRoot>
    2   <customer xmlns="UGLY_NAMESPACE">
    3     <firstName>john</firstName>
    4     <lastName>lennon</lastName>
    5   </customer>
    6   <customer xmlns="UGLY_NAMESPACE">
    7     <firstName>ringo</firstName>
    8     <lastName>starr</lastName>
    9   </customer>
   10 </myRoot>
Posted on Wednesday, December 3, 2008 7:49 AM LINQ | Back to top

Comments on this post: Emitting XML via LINQ

Comments are closed.
Comments have been closed on this topic.
Copyright © Shawn Cicoria | Powered by: