user189198
user189198

Reputation:

Why doesn't a XML attribute have a "parent" in .NET?

So, I'm writing a simple function to remove a XML node from a XML document. The easiest way to achieve this, as far as I can tell, is to:

  1. Get a reference to the node that will be removed (ChildNode)
  2. Get a reference to the node's parent using the ChildNode.ParentNode property (ParentNode)
  3. Call the ParentNode.RemoveChild(ChildNode) method

Now, this works great if the child node is a XmlElement, but what if the child node was a XML attribute? According to the MSDN documentation for XmlNode.ParentNode, the property would return nothing, because "[attributes] do not have parents."

Attributes most definitely do have "parents," do they not? An attribute must be assigned to a XML element, so the XML element would be the attribute's parent, in my mind.

Can someone clear up my misunderstanding, or clarify why the .NET Framework does not see attributes as having parents?

Upvotes: 3

Views: 1715

Answers (2)

Martin Liversage
Martin Liversage

Reputation: 106796

You can use the XmlAttribute.OwnerElement to get the owner of an attribute.

Your procedure will have to be modified to something like this:

  1. Get a reference to the node that will be removed (ChildNode).

  2. If the type of the node is XmlAttribute downcast to that type (AttributeNode) and get a reference to the node's parent using the AttributeNode.OwnerElement property (ParentNode). If not go to step 4.

  3. Call the ParentNode.Attributes.Remove(AttributeNode) method. Skip remaining steps.

  4. Get a reference to the node's parent using the ChildNode.ParentNode property (ParentNode).

  5. Call the ParentNode.RemoveChild(ChildNode) method.

So basically you have to give attributes special treatment reflecting the fact that they are not really part of a parent-child hierarchy but rather - well, attributes - of an XML element.

Upvotes: 11

AakashM
AakashM

Reputation: 63340

A good question, and one where the answer lies in the specification, rather than with Microsoft (for once, some might say...).

Over at the W3C, we can find the specification, which gets to overrule what any one of us might feel is intuitively obvious. In it, I find the word parent only once, in this context:

[Definition: As a consequence of this, for each non-root element C in the document, there is one other element P in the document such that C is in the content of P, but is not in the content of any other element that is in the content of P. P is referred to as the parent of C, and C as a child of P.]

Note that here "element" refers only to actual elements; and "content" refers only to child elements. The XmlNode concept is an abstraction (which does not appear in the specification) which is part of the library, not the standard.

I suppose MS could have made the implementation of ParentNode for attributes return the owning element, but then would you want ChildNodes for an element to include the attributes? That seems less desirable.

Upvotes: 3

Related Questions