CloneNode() public method

public CloneNode ( bool deep ) : XmlNode
deep bool
return XmlNode
        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);
        }
Beispiel #2
0
        public XmlDocument Sign(
            XmlDocument message,
            String nonce,
            DateTime expiresAt
            )
        {
            if (message.DocumentElement == null)
                throw new ArgumentException("No root node in xdoc.");

            var copy = (XmlDocument)message.CloneNode(true);

            SignMessage(
                copy.DocumentElement,
                Convert.FromBase64String(nonce),
                expiresAt,
                new SharedSecret().From(_password)
            );

            return copy;
        }
Beispiel #3
0
        /// <summary>
        /// Make a copy of a XmlDocument
        /// </summary>
        /// <param name="xmlDoc"></param>
        /// <returns></returns>
        private static XmlDocument CloneXmlDoc(XmlDocument xmlDoc)
        {
            if (xmlDoc == null) return null;

            Log.Add(LogTypes.Debug, -1, "Cloning...");
            // Save copy of content
            var xmlCopy = (XmlDocument)xmlDoc.CloneNode(true);
            Log.Add(LogTypes.Debug, -1, "Cloning ended...");
            return xmlCopy;
        }
        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);
        }
Beispiel #5
0
 /// <summary>
 /// Make a copy of a XmlDocument
 /// </summary>
 /// <param name="xmlDoc"></param>
 /// <returns></returns>
 private static XmlDocument CloneXmlDoc(XmlDocument xmlDoc)
 {
     if (xmlDoc == null) return null;            
     
     // Save copy of content
     var xmlCopy = (XmlDocument)xmlDoc.CloneNode(true);            
     return xmlCopy;
 }
		public void CloneNode ()
		{
			XmlDocument doc = new XmlDocument ();
			doc.LoadXml ("<foo><bar /><baz hoge='fuga'>TEST Text</baz></foo>");
			XmlDocument doc2 = (XmlDocument)doc.CloneNode (false);
			AssertEquals ("ShallowCopy", 0, doc2.ChildNodes.Count);
			doc2 = (XmlDocument)doc.CloneNode (true);
			AssertEquals ("DeepCopy", "foo", doc2.DocumentElement.Name);
		}
		protected override void DeserializeElement (XmlReader reader, bool serializeCollectionKey)
		{
			original = new XmlDocument ().ReadNode (reader);
			node = original.CloneNode (true);
		}