What's the difference between XElement and XDocument?

前端 未结 4 895
鱼传尺愫
鱼传尺愫 2020-12-29 04:09

What is the difference between XElement and XDocument and when do you use each?

4条回答
  •  感动是毒
    2020-12-29 04:26

    XDocument represents a whole XML document. It is normally composed of a number of elements.

    XElement represents an XML element (with attributes, children etc). It is part of a larger document.

    Use XDocument when working with a whole XML document, XElement when working with an XML element.

    For example - XElement has a HasAttributes property indicating whether any attributes exist on the element, but an XDocument doesn't, as such a property is meaningless in the context of a whole XML Document.

提交回复
热议问题