我可以将元素转换为XML字符串,然后重新解析它,但我想知道是否有一个更好的方法。
我可以将元素转换为XML字符串,然后重新解析它,但我想知道是否有一个更好的方法。
XElement original = new XElement("original"); XElement deepCopy = new XElement(original);
这里有几个单元测试来演示:
[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); }