Avatar of Nabilbahr
NabilbahrFlag for United States of America

asked on 

how can i add xml node from a file to a node in another xml file

I wanted to move a node with its child nodes to a new xml file , and 2 files have a different structure
Is that possible ? How?
.NET Programming

Avatar of undefined
Last Comment
Nabilbahr
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

1) Load the XML into an XmlDocument

2) Get a reference to an XmlNode.

3) Remove the node from the parent node.

4) Add the node as an XmlElement to the new node.

5) Save the XmlDocument.
Avatar of Nabilbahr
Nabilbahr
Flag of United States of America image

ASKER

this is the error i get
The node to be inserted is from a different document context.
Avatar of Nabilbahr
Nabilbahr
Flag of United States of America image

ASKER


        Dim xmldocl As New XmlDocument
        xmldocl.Load(MapPath("~/App_Data/PSW5.xml"))
        Dim Nodel As XmlElement
        Dim newnode As XmlElement
        Nodel = xmldocl.GetElementsByTagName("FLI_INF").Item(1)
        newnode = Nodel.CloneNode(True)
        ' Create a document fragment.
        Dim docFrag As XmlDocumentFragment = xmldocl.CreateDocumentFragment()
 
 
        Dim xmldoc As New XMLDocument
 
        Dim Node As XmlElement
        xmldoc.AppendChild(xmldoc.CreateXmlDeclaration("1.0", "utf-8", Nothing))
        Node = xmldoc.CreateElement("SESSION")
        xmldoc.AppendChild(Node)
        'xmldoc.AppendChild(Nodel)
        Dim xmldocg As XmlElement
        xmldocg = xmldoc.GetElementsByTagName("SESSION").Item(0)
 
 
        xmldocg.AppendChild(docFrag)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Nabilbahr
Nabilbahr
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo