Xml.CreateTextNode: How to get a trailing space?

How can I get a trailing space in my text node?

  Dim nT As XmlNode = xml.CreateTextNode("section", "hello ")
  nT.SetAttribute("space", "preserve")

Results in an XML file containing:

<section>hello</section>

i.e. no trailing space.

BTW, &nbsp; and other variants such as &#032; do not work, as CreateTextNode properly escapes them.

Can’t be done using Xojo XML libraries I am assuming is the answer?

Jeremy,

Try this instead:

Dim nT As XmlNode =  xml.CreateElement("section")
nT.SetAttribute("space", "preserve")
nT.AppendChild(xml.CreateTextNode("hello "))

ps. You should’t be able to call CreateTextNode with two parameters. Maybe you have overloaded the method and problem lies there.

Sorry, I was camping this weekend. It had been so long since I created the helper CreateTextNode, that I forgot it was one of my creations, however it is not at fault. I started a brand new Console project, no other code in it. In the App.Run event, I placed:

  Dim xml As New XmlDocument
  Dim root As XmlNode = xml.AppendChild(xml.CreateElement("name"))
  Dim fName As XmlNode = xml.CreateElement("first")
  fName.SetAttribute("space", "preserve")
  fName.AppendChild xml.CreateTextNode( "John Doe" )
  root.AppendChild fName
  
  xml.SaveXml(SpecialFolder.Desktop.Child("try.xml"))

and the resulting file is:

<?xml version="1.0" encoding="UTF-8"?><name><first space="preserve">John Doe</first></name>

I believe that Xojo is trimming the string before writing to the file, which is not correct.

I bet it works better if you change “John Doe” to "John Doe "

Duh! Why in the world did I pad the parens instead of the string. Silly me. Yes, that is working great.

Thanks for the help!