我正在使用XmlDocument Class创建一个XML标签内带有一些空格的不合规XML文档。
目标是创建这样的标签:
<name TYPE="Text">Hey</name>
代码:
Dim customNodeName = Tag & " TYPE = " & typestr & ""
Dim customNode As XmlNode = doc.CreateNode("element", customNodeName, "")
因此,当我调试代码时
System.Xml.XmlException
' ' character, hexadecimal value 0x20
被抛出。
有没有可能的解决方法?
答案 0 :(得分:1)
使用下面的代码进行测试。另外,导入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
答案 1 :(得分:1)
由于您已标记此VB,因此请使用XElement。
Dim xe As XElement = <root></root>
xe.Add(<name TYPE="Text">Hey</name>)