XML ImportNode Problem

I’m trying to import XML from one file, into another, and am admitting defeat. Tried every combination of every example in the docs, no avail.

Two XML files. The first, xml1, has existing data with a structure of:

xml2 is just a single structure “record” formatted and structured exactly like those in xml1:
<structure name = “3” … />

I am unable to import or insert “<structure name = “3” … />” from xml2 into xml1.

Simply cannot crack the code. Spent more time than I should have so thought I’d ask!

well, I’ll do it like this:

  1. load in xml2 the structure name=3 like:

Dim xml2 As New XMLDocument xml2.LoadXML struc3 'as string or folderItem
2. load in xml1 the book like:

Dim xml1 As New XMLDocument xml1.LoadXML book 'as string or folderitem
3. import the root node from xml2 into root node of xml1, like:

Dim theNode As XMLNode= xml1.ImportNode(xml2.FirstChild, True) xml1.FirstChild.FirstChild.AppendChild theNode

Thank you for the assistance, Bernardo! Progress. I am able to add the record from xml2 into xml1, but not in the proper location. Based on what you wrote I think the problem is that there are other elements of other names before and after the “<structures … />” and I need to be able to identify where in xml1 to append. My original post was very simplified and that made it less clear.

What I need is something like the following pseudocode:

Dim theNode As XMLNode= xml1.ImportNode(xml2.FirstChild, True) xml1.Child("structures").AppendChild theNode

However I cannot find a way of directly addressing it.

I guess I could use a loop to “find” the proper child and then .AppendChild to it?


Wow, I knew it had to be something simple. The finished code is below.

[code]Dim nodes As XmlNodeList
Dim node As XmlNode

node = xml1.ImportNode(xml2.FirstChild, True) ’ import and return imported “node”
nodes = xml1.XQL("//book/structures/structure") ’ the collection of structure records
nodes.Item(nodes.Length - 1).AppendChild(node) ’ append to the end of the collection

Sadly, the above code is not working in practice. Turns out that it inserts the new record INTO the last record. Not AFTER the last record and within bounds of the “structures” collection.


What a journey. Final code. Simple.

node = xml1.ImportNode(xml2.FirstChild, True) ' the imported node nodes = xml1.XQL("//book/structures") ' the collection of structure records nodes.Item(0).AppendChild(node) ' append to the end of the collection