具有关联的XElement的XDocument/Element的深层复制 [英] Deep Copy of XDocument/Element with associated XElement (s)

查看:270
本文介绍了具有关联的XElement的XDocument/Element的深层复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我有一个XDocument

Ok I have a XDocument

BaseDocument = XDocument.Load(@".\Example\Template.xml");

和由方法生成的XElement的一些DataStructure(在XDocument内部).这只是一个例子:

and some DataStructure of XElements (inside the XDocument) that gets generated by a method. This is just an example:

Dictionary<string, List<XElement>> ElementMap = GetElementMapping(BaseDocument);

我想同时复制两者,

有没有比这更有效的方法,

Is there a more efficient way than,

XDocument copy = new XDocument(BaseDocument);
Dictionary<string, List<XElement>> copyElementMap = GetElementMapping(copy);

要复制数据结构,以便内部的XElements引用新副本?

to copy the datastructure so that the XElements inside reference the new copy?

我拍了一些照片来展示我想要的东西

I made some pictures to show what I want:

当前解决方案:

I-Want解决方案:

推荐答案

就您制作的XDocument副本而言,它是可以肯定的,那么我们肯定会确保它尽可能快地运行,

As far as the XDocument copy you make is concearned, then we know that for sure it is as fast as we can go, as we can see from the documentation at line 2320. This does a deep copy the way we want it to do.

如果您需要对XDocument对象进行深层复制,则以上是有关性能的最佳方法.它对文档中的每个节点(包括XElements,XAttributes,注释等)执行深层复制,而无需重新加载文件.它在内存中读取并克隆所有节点.这是一种高效的操作,也是我们所能实现的最高效的操作,因为它会自动禁止通常在XDocument内部触发的所有通知事件.可以从以下位置验证深层副本:

If you need to do a deep copy of the XDocument object, then the above is the best way to go with regards to performance. It performs a deep copy of every node in the document (including XElements, XAttributes, comments etc.) without having to reload the file. It reads and clones all nodes while in-memory. This is an efficient operation, and it is the most efficient we can have, as it automatically suppresses all notification events that are normally fired internally in the XDocument. The deep copy can be verified from the below:

使用的XML:

<?xml version="1.0" encoding="utf-8" ?>
<FirstNode>
  <ChildNode attributeOne="1"/>
</FirstNode>

源代码

XDocument xDoc = XDocument.Load("AnXml.xml");
XDocument copy = new XDocument(xDoc);

Console.WriteLine("xDoc before change copy: {0}", xDoc.ToString());

copy.Root.Add(new XElement("NewElement", 5));
copy.Element("FirstNode").Element("ChildNode").Attribute("attributeOne").SetValue(2);
Console.WriteLine("xDoc after change copy: {0}", xDoc.ToString());
Console.WriteLine("copy after change copy: {0}", copy.ToString());

Console.ReadKey();

对Console.WriteLine的两次调用输出不同的值,表明这两个引用指向具有不同结构的不同项目,证明已进行了深拷贝.

The two calls to Console.WriteLine output different values indicating that the two references point to different items with different structure, proving that a deep copy was made.

请注意,如果您想重复使用现有的XElement,则无法在不使用反射的情况下将它们设置到XDocument中:在XDocument中设置XElement的所有公共方法都将执行深层复制.从我包含的链接(.Net源代码)中可以明显看出这一点.

Note that if you want to re-use the XElements you have, there is no way to set them into XDocument without using reflection: all public methods to set XElements in an XDocument perform a deep copy. This is evident from the link I included, which is the .Net source code.

这篇关于具有关联的XElement的XDocument/Element的深层复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆