What's the difference between XElement.Load and XDocument.Load?

前端 未结 3 1167
终归单人心
终归单人心 2020-12-30 19:37

As stated above, what\'s the difference between XElement.Load and XDocument.Load? They both seemingly load an XML file.

相关标签:
3条回答
  • 2020-12-30 20:03

    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

    0 讨论(0)
  • 2020-12-30 20:04

    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.

    0 讨论(0)
  • 2020-12-30 20:28

    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 via XElement.Load.

    0 讨论(0)
提交回复
热议问题