views:

98

answers:

1

Assume we have some DataTable or IEnumerable with ChildID, ParentID and Title. We need to serialize it to XML like

<Entity title="">
   <Entity title=""></Entity>
   <Entity title=""></Entity>
</Entity> 

As i found out, standard DataTable.GetXML() returns something different. I thought about initializing class tree like Entity e = new Entity(ID, Title, ParentEntityID) and then serializing it. Table is about > 3000 elems. Is there faster way?

+1  A: 

Selecting with Linq to SQL into Linq to XML

var document = new XDocument( 
   (
    from f in db.FirstTable
    select new XElement("Entity", 
                new XAttribute("title", f.TitleField),
                (            
                  from s in f.SecondTable
                  select new XElement("Entity",
                            new XAttribute("title", f.TitleField),
                            new XAttribute("Entity", f.SomeField)
                  )
                ).ToArray()
           )
   ).ToArray()
);

XElement constructor, as well as XDocument's, accepts an array or content, that's why the .ToArray().

Alex Bagnolini
that's may be a decision, but we have still 2.0 on production servers :). I will try with XMLDocument maybe.
rudnev
You can compile it for .NET 2.0, no? (Correct me if it doesn't work with LINQ please)
Alex Bagnolini