473,327 Members | 2,007 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Ask for xmlelement copy method

Hello everyone.
I have two instance of xml.xmldocument. One is XA another is XB. XA has a
child node XA1. XA1 has many attributes and child nodes. Now I want to copy
XA1 to XB with all of it's attributes and child nodes. In .NET, the type of
XA1 is xml.xmlelement.
If I use clone method. The copy of XA1 is belong to XA. I can not append it
to XB.
If I careate a new instance from XB.CreateElement method. I need to copy
every attribute of XA1 to the new xml.xmlelement instance. The XA1.InnerXML
property just return the child nodes of XA1, and the XA1.OuterXML is
readonly. I can not send it to the new instance.
Who know how can i copy XA1 to XB easily and efficiently. Thanks.

Steven
Nov 12 '05 #1
4 3852
Steven.Xu wrote:
I have two instance of xml.xmldocument. One is XA another is XB. XA has a
child node XA1. XA1 has many attributes and child nodes. Now I want to copy
XA1 to XB with all of it's attributes and child nodes. In .NET, the type of
XA1 is xml.xmlelement.


Take a look at XmlDocument.ImportNode() method.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #2
> Take a look at XmlDocument.ImportNode() method.

Thanks for your help. but i said that the type of the instance which i want
to copy is xml.xmlelement. Not xml.xmldocument. So it's not have the
ImportNode method. Do you have another way? Thanks again!
Nov 12 '05 #3
"Steven.Xu" <St******@discussions.microsoft.com> wrote in message news:70**********************************@microsof t.com...
Take a look at XmlDocument.ImportNode() method.


Thanks for your help. but i said that the type of the instance which i want
to copy is xml.xmlelement. Not xml.xmldocument. So it's not have the
ImportNode method. Do you have another way? Thanks again!


The reason AppendChild( ) the deep-cloned XmlElement of 'XA1' to
'XB' is because the cloned node, like the original, is "owned" by
XmlDocument 'XA.' The ImportNode( ) method allows developers
to change the ownership of the node when moving nodes between
two documents.

By calling ImportNode( ) on 'XB' to change the ownership of the cloned
node from 'XA' to 'XB', you will then be able to AppendChild( ) the node
that has been imported to 'XB'. Here's an example,

XmlDocument XA = new XmlDocument( );
XA.Load( "yesterdaysLog.xml");
XmlDocument XB = new XmlDocument( );
XB.Load( "todaysLog.xml");

XmlElement XA1 = XA.DocumentElement.FirstChild;
XmlElement XA1Clone = XA1.CloneNode( true);
XmlElement XB1 = XB.ImportNode( XA1Clone);

// Works as expected after the cloned element has been imported into XB.
XB.DocumentElement.AppendChild( XB1);
Derek Harmon
Nov 12 '05 #4
Thank you very much for your help! Than's GOOD!
Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Parham | last post by:
Hello, I have an inherited class from XmlElement with this name: XmlElementCF I have added a function in XmlElementCF that returns a XmlNode, the node type is Element. I can cast the returned node...
1
by: john diss | last post by:
hello there everyone.. I have created a class called "ProcessLog" inheriting from XmlDocument and two classes ("UploadedItem", "ProcessError") inheriting from XmlElement. I then have two...
2
by: Compic | last post by:
Hi! I have this class: public class StoredItemElement : XmlElement { protected internal StoredItemElement(string prefix, string localName, string namespaceUri, XmlDocument doc) :...
0
by: ryanr | last post by:
I've run into a problem with a recursive algorithm. Basically, it grabs a tree, and creates premutations based off it. For example: Suppose you have you sell shoes in two colors (Black,...
0
by: Casper Bang | last post by:
I set up a Document-Literal(wrapped) WebService on a Oracle container (J2EE 1.4/JAX-RPC) and made a basic method which I can invoke from a browser or Java client. Java prototype: public...
1
by: RK | last post by:
What is the difference in performance if using XmlDocument or XmlElement as parameter in C# class method. thanks in advance. --RK
4
by: Varangian | last post by:
Hello something I want to ask...... why on earth you cannot inherit from XmlElement???? .... or you can but XmlElement constructor is protected or private and so you cannot define an empty...
1
by: bullrout | last post by:
Hi There, I have a method which uses some values passed in as xml, at times some of the elements in the document will be null. The document itself has many elements and I wanted to handle the null...
2
by: nygiantswin2005 | last post by:
Hi I have wrote a method in my C# class with the following code, to create an XMl file. The root element is suppose to have 3 attibutes. I can not get attributes to appear correctly. ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.