string to xmlnode

Does anyone know a simple way to convert a string of a former xmlNode to a new xmlNode? Something like:

Var xml As New XmlDocument

Var root As XmlNode
root = xml.AppendChild(xml.CreateElement("League"))

Var teamNode As XmlNode
Var teamString As String

teamString = teamNode.toString
// teamNode = teamString.toXML (but of course I just wish that would work)

I created a function for this. But maybe there is a more simple way?

Function stringToXMLNode(s As String) As xmlNode
  Var xml As New XmlDocument
Try
  xml.LoadXml(s)
Catch XMLError As XmlException
  MessageBox("XML error: " + XMLError.Message)
End Try

return xml
End Function

see Extends

Thanks Markus! I wasn’t aware of that great function. I now placed my method in a module and set the parameters to “extends s as string”. But if I run the script, I get the message, that the method requires a conversion from type Variant to type string. “.StringValue” didn’t help. Any hints?

It would be helpful to see your extends method (in case you made any internal changes) and exactly where that error occurs.

I’m guessing that the error is from where you are calling your new method. In which case you’ll probably need to Cast it or assign it to a string variable first.

[quote=478350:@Greg O’Lone] Var xml As New XmlDocument
Try
xml.LoadXml(s)
Catch XMLError As XmlException
MessageBox("XML error: " + XMLError.Message)
End Try

return xml[/quote]
I use exactly the same code as above:

 Var xml As New XmlDocument
Try
  xml.LoadXml(s)
Catch XMLError As XmlException
  MessageBox("XML error: " + XMLError.Message)
End Try
return xml

To call it I use

ListboxLibrary.CellTagAt(ListboxLibrary.RowCount-1,0).toXML

… and it would be helpful to see what’s the contents of your teamString… or teamNode.
A string should be like the team name to be successfully converted in a XML doc.

And in this case you should have something like: //just to focus on content, there is not the needed try/catch
var xDoc as new xmlDocument
xDoc.loadXML(theString) //here the string is the team name
var teamNode as XmlNode=xDoc.documentElement
return teamNode.toString //or simply return teamNode if you need the node

you can also create the node from the string (team name) if you know the node name needed:
var xDoc as new xmlDocument
var teamNode as xmlNode=xDoc.createElement(“team”) //the node name
teamNode.appendChild xDoc.createTextNode(“team name”)
return teamNode.toString //or simply return teamNode if you need the node

In any case if you use this “one shot” node in another xmlDocument remember to append in the right place the node as an importedOne.
or if you only want to add your team to the league doc and you have the node from another xmldocument or (that’s the same) you create your node
var myOtherNode as xmlNode //some node in another xmlDocument (otherDoc)
myOtherNode.appendChild otherDoc.import(teamNode, true) //use true if you need the entire sub tree or false for only the node

@Antonio Rinaldi the team name the node looks like this, yes. the return type of the method is xmlNode, so I don’t have to convert it back to string. The idea is to convert a string to an xmlNode type. The parameters of the method is

extends s As string

this brings solution:

extends s As variant

Var xml As New XmlDocument Try xml.LoadXml(s.StringValue) Catch XMLError As XmlException MessageBox("XML error: " + XMLError.Message) End Try return xml

ListboxLibrary.CellTagAt(ListboxLibrary.RowCount-1,0).toXML

seems this method return a variant and not a string.