使用 XmlDocument Class 创建带有空格的 Xml 标签
Using XmlDocument Class to create Xml tags with whitespaces
我正在使用 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
被抛出。
是否有可能的解决方法?
使用下面的代码进行测试。此外,进口 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
上面的代码将产生:
因为你已经标记了这个 VB 使用 XElement。
Dim xe As XElement = <root></root>
xe.Add(<name TYPE="Text">Hey</name>)
我正在使用 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
被抛出。
是否有可能的解决方法?
使用下面的代码进行测试。此外,进口 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
上面的代码将产生:
因为你已经标记了这个 VB 使用 XElement。
Dim xe As XElement = <root></root>
xe.Add(<name TYPE="Text">Hey</name>)