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()
.
精彩评论