As stated above, what's the difference between XElement.Load
and XDocument.Load
? They both seemingly load a开发者_开发技巧n XML file.
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
.
There are not many scenarios that require you to create an XDocument. Instead, you can usually create your XML trees with an XElement root node. Unless you have a specific requirement to create a document (for example, because you have to create processing instructions and comments at the top level, or you have to support document types), it is often more convenient to use XElement as your root node.
Straight from Valid Content of XElement and XDocument Objects. As far as the Load methods...they both load content into their respective objects.
There is a difference while loading comments it will not be loaded in case of XElement.Load() While The XDocument.Load() will also get Comment
Hence while working with Xlinq we should go with XDocument
精彩评论