开发者

Fastest way of creating XML from parent-child table in c#?

开发者 https://www.devze.com 2022-12-25 21:47 出处:网络
Assume we have some DataTable or IEnumerable with ChildID, ParentID and Title. We need to serialize it to XML like

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 t开发者_如何转开发here faster way?


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().

0

精彩评论

暂无评论...
验证码 换一张
取 消