Why doesn't a XML attribute have a "parent" in .NET? -
so, i'm writing simple function remove xml node xml document. easiest way achieve this, far can tell, to:
- get reference node removed (
childnode
) - get reference node's parent using
childnode.parentnode
property (parentnode
) - call
parentnode.removechild(childnode)
method
now, works great if child node xmlelement
, if child node xml attribute? according msdn documentation xmlnode.parentnode
, property return nothing
, because "[attributes] not have parents."
attributes have "parents," not? attribute must assigned xml element, xml element attribute's parent, in mind.
can clear misunderstanding, or clarify why .net framework not see attributes having parents?
you can use xmlattribute.ownerelement
owner of attribute.
your procedure have modified this:
get reference node removed (
childnode
).if type of node
xmlattribute
downcast type (attributenode
) , reference node's parent usingattributenode.ownerelement
property (parentnode
). if not go step 4.call
parentnode.attributes.remove(attributenode)
method. skip remaining steps.get reference node's parent using
childnode.parentnode
property (parentnode
).call
parentnode.removechild(childnode)
method.
so have give attributes special treatment reflecting fact not part of parent-child hierarchy rather - well, attributes - of xml element.
Comments
Post a Comment