简体   繁体   中英

Linq2Xml XAttribute hierarchy location?

Im having trouble understanding this :

在此处输入图片说明

why Microsoft put Xattribute under Xobject ?

they say that :

Attributes are not derived from XNode; they are not nodes in the XML tree. Instead, they are simply name/value pairs associated with an element.

fine ...With an element.

so why not where I made a draw ?

Attributes belong to an Element.

Using inheritance would mean that "an attribute is a (special kind of) Element", which is obviously incorrect. An attribute cannot have children for example.

The tree is an inheritance tree, it really doesn't show how the objects are used together.

The location you are suggesting just doesn't work from an inheritance stand point. Elements can have children and attributes, so if XAttribute was derived from XElement, it would mean that Attributes could have child elements and their own attributes. The same goes with XContainer, so it doesn't make sense to place XAttribute next to XElement in the tree either.

XNode is the next step up. Could XAttribute fit under XNode? Not really. When you look at an XML, in a general sense, there are 2 basic structural elements, Nodes and Attributes. Neither is derived from the other so it makes sense that XAttribute and XNode be on different branches of the XObject tree.

That just leaves XObject, which is the base Xml2Linq object type and everything must derive from it, so that is the best (and really only appropriate location) for the XAttribute type.

这是一个继承树,并且XAttribute是从XObject派生的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM