The difference is that an XElement
type represents an XML fragment while the XDocument
type represents an entire XML document with all associated meta-data.
That being said however, for most simple cases you can use them interchangeably.
It is important to understand the subtle differences in querying between these two types and for more information on that I would recommend that you please read Querying an XDocument vs. Querying an XElement:
When you load a document via
XDocument.Load
, you will notice
that you have to write queries
slightly differently than when you
load viaXElement.Load
.