views:

771

answers:

1

I have a class that is marked with DataContract attributes and I would like to create an XDocument from objects of that class. Whats the best way of doing this?

I can do it by going via an XmlDocument but this seems like an unnecessary step.

+4  A: 

You can create an XmlWriter directly into the XDocument:

XDocument doc = new XDocument();
using (var writer = doc.CreateWriter())
{
    // write xml into the writer
}
Console.WriteLine(doc.ToString());
marklam
You are correct; I'm removing my answer
Marc Gravell
hmm... it seems I can't delete an accepted answer... I'll add a comment to the OP instead...
Marc Gravell