Pickeroll
Pickeroll

Reputation: 978

Using XmlDocument Class to create Xml tags with whitespaces

I'm using the XmlDocument Class to create a non compliance XML Document with some whitespace inside the XML tags.

The aim is to create a tag like this:

<name TYPE="Text">Hey</name>

Code:

Dim customNodeName = Tag & " TYPE = " & typestr & ""
Dim customNode As XmlNode = doc.CreateNode("element", customNodeName, "")

So when I debug my code a

 System.Xml.XmlException
 ' ' character, hexadecimal value 0x20

Is thrown.

Is there a possible workaround ?

Upvotes: 0

Views: 69

Answers (2)

dbasnett
dbasnett

Reputation: 11773

Since you have tagged this VB use XElement.

    Dim xe As XElement = <root></root>
    xe.Add(<name TYPE="Text">Hey</name>)

Upvotes: 1

Epistaxis
Epistaxis

Reputation: 211

Use the code below to test out. Also, Imports System.Xml :)

    ' Just used console app for demo purposes
Sub Main()
    ' Create an XmlDocument to house the stuff
    Dim doc As New XmlDocument

    ' Create your root element
    Dim root As XmlElement = doc.CreateElement("root")

    ' Create your 'name' element
    Dim name As XmlElement = doc.CreateElement("name")

    ' Set the attribute of 'name' to nothing as your example has.
    name.SetAttribute("Type", "Text")

    ' Set the innerText of your name element as your example has.
    name.InnerText = "Hey"

    ' Append your creations
    root.AppendChild(name)
    doc.AppendChild(root)

    ' This is only here for review
    doc.Save("C:\Temp\Test.xml")
End Sub

The code above will yield: enter image description here

Upvotes: 1

Related Questions