I have two methods: One creates an XMLDocument. It calls another method that creates a node to insert into that document. I have these separate because there are situations where I need to handle the node creation a little differently.
The structure of my finished XML file is very simple:
<?xml version="1.0" encoding="UTF-8"?>
<RootNode version="1.0">
<BurnIn name="Default">
<Item xloc="0.02" yloc="0.98"/>
</BurnIn>
</RootNode>
This is the method that generates that file:
Public Function BuildScannerXMLFile() As XMLDocument {
Var xml As New XMLDocument
Var root As XMLNode
root = xml.AppendChild(xml.CreateElement("RootNode"))
root.SetAttribute("version", "1.0")
//Create a new XMLDocument with just a BurnIn node
var BurnInNode as XMLDocument
BurnInNode = BuildXMLBurnInNode
//Import the BurnIn node from the new file into the master XML document
var importedNode as XMLNode
importedNode = xml.ImportNode(BurnInNode.DocumentElement, True)
root.AppendChild(importedNode)
return xml
}
And the node creation method:
Public Function BuildXMLBurnInNode(name as string = "", desc as string = "") As XMLDocument {
var xml as new XMLDocument
Var BurnIn As XMLNode
BurnIn = xml.AppendChild(xml.CreateElement("BurnIn"))
//Skipping the code that handles name and desc arguments, for brevity here
Var item as XMLNode
item = burnin.AppendChild(xml.CreateElement("item"))
item.SetAttribute("xloc", "0.02" )
item.SetAttribute("yloc", "0.98" )
return xml
This works perfectly: BuildScannerXMLFile generates a fully formed XML file thatās exactly what I want in that situation.
Now elsewhere, I want to re-use the BuildXMLBurnInNode method in a couple places. For example, when constructing a master XML file that contains multiple nodes:
In a button, where you assign a name and description to one of these BurnIn nodes:
if tLibraryItemName.Text <> "" then
//Put the current library of burnins into a local variable, for convenience
var workingLibrary as XMLDocument
workingLibrary = MainWindow.LibraryXML
//Generate the Burnin Node, and add the name and description to it
Var Burnin As New XmlDocument
Burnin = BuildXMLBurnInNode(tLibraryItemName.Text, tLibraryItemDesc.Text)
//Extract the BurnInNode for insertion into the library XML
var importedNode as XMLNode
importedNode = Burnin.ImportNode(Burnin.DocumentElement, True)
//This is where it fails
workingLibrary.AppendChild(importedNode)
me.Close
else
beep
if tLibraryItemName.Text = "" then
tLibraryItemName.BackgroundColor = color.red
end if
end if
This fails at workingLibrary.AppendChild(importedNode) ā I keep getting an XMLDomException in the debugger (error code 4, "node used in a document that did not create it"). Iāve also tried using InsertNode, but I get the same error.
So in the BuildScannerXMLFile method I have access to the ārootā element, and Iām appending to that. In the button I donāt, and I think thatās why itās failing. So how do I insert the Burnin node into the workingLibrary XML document?