调用 XElement.Remove() 后,C#/Linq 是否可以将 XAttribute 添加到 XElement?
我目前正在将 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调用
Remove()
只是将元素从其父元素中拉出。该元件本身将继续正常工作。
Calling
Remove()
just pulls the element out of its parent.The element itself will continue to work fine.