本文整理汇总了C#中XmlDocument.CloneNode方法的典型用法代码示例。如果您正苦于以下问题:C# XmlDocument.CloneNode方法的具体用法?C# XmlDocument.CloneNode怎么用?C# XmlDocument.CloneNode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类XmlDocument
的用法示例。
在下文中一共展示了XmlDocument.CloneNode方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CloneComplexDocumentTrue
public static void CloneComplexDocumentTrue()
{
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(ComplexDocument);
var cloned = xmlDocument.CloneNode(true);
Assert.NotSame(xmlDocument.ChildNodes, cloned.ChildNodes);
Assert.Equal(xmlDocument.NodeType, cloned.NodeType);
Assert.Equal(xmlDocument.ChildNodes.Count, cloned.ChildNodes.Count);
Assert.Equal(xmlDocument.LastChild.Value, cloned.LastChild.Value);
// Test XmlNode.LastChild
{
for (var i = 0; i < xmlDocument.ChildNodes.Count; i++)
{
if (xmlDocument.ChildNodes[i].LastChild == null)
{
Assert.Equal(xmlDocument.ChildNodes[i].LastChild, cloned.ChildNodes[i].LastChild);
}
else
{
Assert.Equal(xmlDocument.ChildNodes[i].LastChild.Value, cloned.ChildNodes[i].LastChild.Value);
}
}
}
// Test XmlNode.NextSibling
{
var count = cloned.ChildNodes.Count;
var previousNode = cloned.ChildNodes[0];
for (var idx = 1; idx < count; idx++)
{
var currentNode = cloned.ChildNodes[idx];
Assert.Equal(currentNode, previousNode.NextSibling);
previousNode = currentNode;
}
Assert.Null(previousNode.NextSibling);
}
// Test XmlNode.PreviousSibling
{
var count = cloned.ChildNodes.Count;
var nextNode = cloned.ChildNodes[count - 1];
for (var idx = count - 2; idx >= 0; idx--)
{
var currentNode = cloned.ChildNodes[idx];
Assert.Equal(currentNode, nextNode.PreviousSibling);
nextNode = currentNode;
}
Assert.Null(nextNode.PreviousSibling);
}
// Test XmlNode.ParentNode
CheckChildren(cloned);
}
示例2: CloneComplexDocumentTrueAndManipulate
public static void CloneComplexDocumentTrueAndManipulate()
{
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml("<a>test</a>");
var cloned = xmlDocument.CloneNode(true);
cloned.FirstChild.FirstChild.Value = "replaced";
Assert.Equal("test", xmlDocument.FirstChild.FirstChild.Value);
Assert.Equal("replaced", cloned.FirstChild.FirstChild.Value);
}