我想制作一个LINQ to XML XElement的深层副本.我想这样做的原因是文档中有一些节点我想创建(在同一文档中)的修改副本.我没有看到这样做的方法.
我可以将元素转换为XML字符串然后重新解析它,但我想知道是否有更好的方法.
Jon*_*att 119
没有必要重新解析.XElement的构造函数之一采用另一个XElement并对其进行深层复制:
XElement original = new XElement("original");
XElement deepCopy = new XElement(original);
Run Code Online (Sandbox Code Playgroud)
这里有几个单元测试来演示:
[TestMethod]
public void XElementShallowCopyShouldOnlyCopyReference()
{
XElement original = new XElement("original");
XElement shallowCopy = original;
shallowCopy.Name = "copy";
Assert.AreEqual("copy", original.Name);
}
[TestMethod]
public void ShouldGetXElementDeepCopyUsingConstructorArgument()
{
XElement original = new XElement("original");
XElement deepCopy = new XElement(original);
deepCopy.Name = "copy";
Assert.AreEqual("original", original.Name);
Assert.AreEqual("copy", deepCopy.Name);
}
Run Code Online (Sandbox Code Playgroud)
看起来像ToString和重新分析方法是最好的方法.这是代码:
XElement copy = XElement.Parse(original.ToString());
Run Code Online (Sandbox Code Playgroud)
简而言之,直接从 C# 3.0 提升:
\n\n当将节点或属性添加到元素时(无论是通过函数构造还是 Add 方法),节点或属性的 Parent 属性将设置为该元素。一个节点只能有一个父元素:如果将一个已成为父节点的节点添加到第二个父节点,该节点将自动深度克隆。在以下示例中,每个客户都有一个单独的地址副本:
\n\nvar address = new XElement ("address",\n new XElement ("street", "Lawley St"),\n new XElement ("town", "North Beach")\n );\nvar customer1 = new XElement ("customer1", address);\nvar customer2 = new XElement ("customer2", address);\n\ncustomer1.Element ("address").Element ("street").Value = "Another St";\nConsole.WriteLine (\n customer2.Element ("address").Element ("street").Value); // Lawley St\nRun Code Online (Sandbox Code Playgroud)\n\n这种自动复制使 X-DOM 对象实例化没有副作用\xe2\x80\x94另一个函数式编程的标志。
\n| 归档时间: |
|
| 查看次数: |
17923 次 |
| 最近记录: |