Think I've found an xml document bug, but it could be me...

I’ve been trying to add/update and delete nodes from an xml string.
However (as this project demonstrates) after adding a second node to an xml document the document becomes corrupt.

I’ve attached a simple project that demonstrates the issue.

There is one button that steps through tests…

The first press adds a node to the document. (Successfully)
The second press adds a second node to the document (and the document gets corrupted.)
The third press deletes (or it would if the document wasn’t corrupted.) a nod.

link to xojo project

This method I suspect is the culprit…

[code]Private Sub UpdateCfg(pipe as string, Tag as String, mode as updateOperation)
dim ExistingNode, NewNode as XmlNode
dim cfgxml, pipelinexml as XmlDocument

if mCfg <> “” then cfgxml = new XmlDocument(mCfg)
if pipe <> “” then pipelinexml = new XmlDocument(pipe)

if cfgxml <> nil and Tag <> “” then
ExistingNode = DoesPipeLineExist(Tag, cfgxml)
end if
if pipelinexml <> nil then
NewNode = cfgxml.ImportNode(pipelinexml.FirstChild, true)
end if

select case mode
case updateOperation.delete
if ExistingNode <> Nil then
end if
case updateOperation.update
if NewNode <> nil and ExistingNode <> Nil then
cfgxml.FirstChild.ReplaceChild(NewNode, ExistingNode)
end if
case updateOperation.add
if NewNode <> Nil then
end if
end select

mCfg = “”
DumpXml(cfgxml.FirstChild, 0, mCfg)

End Sub[/code]

The problem is DumpXML isn’t rebuilding the XML correctly. You’re getting “#text” appearing because that’s the Name of an XmlTextNode. To get the actual text use Value. Here’s a crude fix…

[code] //snip
ts1 = ts + " "

if n IsA XmlTextNode then
s = s + ts + n.Value

s = s + ts+"<"+n.Name+">" + EndOfLine.Unix
s = s + ts + "</" + n.Name + ">" + EndOfLine.Unix


Easiest fix is to use (as the entirety of the method)

s = n.ToString

Stuart thank you so much for your reply, but I’m having trouble syncing my version of xmldump with the changes you suggested…
(Sorry didn’t undestand your notation.)

[code]Private Sub DumpXml(n as XmlNode, level as integer, byref s as String)
Dim xAtt As XmlAttribute
dim ts, ts1 as string // Tabs or spaces
dim i as integer

for i=0 to level-1
ts = ts + " " //chr(9)
ts1 = ts + " "

s = s + ts+"<"+n.Name+">" + EndOfLine.Unix

for i=0 to n.AttributeCount-1
xAtt = n.GetAttributeNode(i)
s = s + ts1 + xAtt.Name + “=” + chr(34) + xAtt.Value + chr(34) + EndOfLine.Unix

for i=0 to n.ChildCount-1
DumpXml(n.Child(i), level+1, s)

s = s + ts + “</” + n.Name + “>” + EndOfLine.Unix

End Sub
I did of course see the ToString method, but it lacks the formatting that I need…

Ahh now I understand stuart … Thanks that is exactly it!

However this still is broken… Deletion doesn’t work because I can’t find the node…