Can C# / Linq add an XAttribute to a XElement after the XElement.Remove() has been invoked?

868 views Asked by At

I'm currently translating portions of the chem4word project from C# to AS3.

In the below code the XElement DelegateElement calls its Remove() method. If mark == true then it passes that XElement to have an XAttribute "deleted" = "true" but if the node has been removed what does c# / Linq actually do?

        private XElement DelegateElement;

        public void DeleteSimple(bool mark)
        {
            if (IsDeleted())
            {
                Log.Debug("WARN: Trying to delete previously deleted CMLElement " + GetTag());
            }
            else
            {
                if (this.DelegateElement.Parent != null)
                {
                    ///////////////////////////////////////////
                    // Here, if the XElement is being removed,
                    // where does the MarkAsDeleted method add 
                    // the XAttribute?
                    ////////////////////////////////////////////
                    this.DelegateElement.Remove();
                    if (mark)
                    {
                        MarkAsDeleted(this.DelegateElement);
                    }
                }
            }
        }


        private void MarkAsDeleted(XElement delegateElement)
        {
            // Deleted = "deleted; True = "true" 
            delegateElement.Add(new XAttribute(Deleted, True));
        }

just for 'fun' here is the as3 version of the code. Following the above c# way, the deleted node will not get a new attribute (or any new information) added to the xml.

        private var DelegateElement:XML;
        public function DeleteSimple(mark:Boolean):void
        {
            if( IsDeleted() )
            {
                // log output
            }
            else
            {
                if( this.DelegateElement.parent() != null )
                {
                    delete this.DelegateElement.parent().children()[this.DelegateElement.childIndex()];
                    if( mark )
                    {
                        MarkAsDeleted(this.DelegateElement);
                    }
                }
            }
        }

        private function MarkAsDeleted(delegateElement:XML):void
        {
            delegateElement['@'+Deleted] = True;
        }
1

There are 1 answers

1
SLaks On BEST ANSWER

Calling Remove() just pulls the element out of its parent.
The element itself will continue to work fine.