我正在尝试进行一些XSLT转换,使用以下代码行将XML转换为XML。当我尝试从转换后的XML创建XMLDocument对象时,我收到错误
根级别的数据无效。第1行,第1位。
Dim outputXML As New XmlDocument
Dim stream As New MemoryStream
Dim writer As XmlTextWriter = New XmlTextWriter(stream, System.Text.UnicodeEncoding.UTF8)
Dim navigator As XPathNavigator = illustratePlusXML.CreateNavigator()
Dim transormer As XslCompiledTransform = New XslCompiledTransform()
transormer.Load(ConfigurationManager.AppSettings("XSLT_File_Path"))
transormer.Transform(navigator, Nothing, writer)
Dim output As String = System.Text.UnicodeEncoding.UTF8.GetString(stream.ToArray())
outputXML.LoadXml(output)
Return outputXML
我可以找到一个特殊字符(方框),我认为这会导致错误。输出xml的附加快照。有人可以建议吗?
答案 0 :(得分:2)
如果您希望填充XmlDocument
作为XSLT转换的结果,那么只需执行
Dim resultDoc As New XmlDocument()
Using xw As XmlWriter = resultDoc.CreateNavigator().AppendChild()
Dim navigator As XPathNavigator = illustratePlusXML.CreateNavigator()
Dim transormer As XslCompiledTransform = New XslCompiledTransform()
transormer.Load(ConfigurationManager.AppSettings("XSLT_File_Path"))
transormer.Transform(navigator, Nothing, xw)
xw.Close()
End Using
无需使用MemoryStream。如果您确实认为需要使用MemoryStream,请确保在调用Position
方法之前将其0
重置为Load
。