调用 XElement.Remove() 后,C#/Linq 是否可以将 XAttribute 添加到 XElement?

发布于 2024-10-17 08:28:57 字数 2179 浏览 6 评论 0原文

我目前正在将 chem4word 项目的部分内容从 C# 翻译为 AS3。

在下面的代码中,XElement DelegateElement 调用其 Remove() 方法。如果 mark == true 那么它会传递该 XElement 以获得 XAttribute "deleted" = "true" 但如果该节点已被删除c# / Linq 真的可以吗?

        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));
        }

只是为了“好玩”,这里是代码的 as3 版本。按照上述c#方式,删除的节点将不会获得添加到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;
        }

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;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转身以后 2024-10-24 08:28:57

调用 Remove() 只是将元素从其父元素中拉出。
该元件本身将继续正常工作。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文