我试图读取xml文件并将其加载到xml文档中。但是,错误提示我根级别的"数据无效。第1行,第1位。"我需要从xml文件中检索消息。
以下是我的代码:
Dim xmlPath as String = Server.MapPath("~/res.xml")
Dim xmlDoc as New XmlDocument
Dim fs As New FileStream(xmlPath, FileMode.Open, FileAccess.Read)
xmlDoc.Load(fs)
以下是xml文件的内容:
--uuid:8asdsddf-sdf24-asdasd-3121-asdasdasdasd
Content-Type: application/xop+xml; charset=UTF-8; type="text/xml";
Content-Transfer-Encoding: binary
Content-ID: <root.message@apache.org>
<SOAP-ENV:Envelope xmlns:SOAP-
ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header></SOAP-
ENV:Header><SOAP-ENV:Body><Status xmlns=""><Message>Success: 12345</Message></Status></SOAP-ENV:Body></SOAP-
ENV:Envelope>
--uuid:8asdsddf-sdf24-asdasd-3121-asdasdasdasd--
错误提示的原因是什么?
答案 0 :(得分:0)
以下代码应删除文件
中的所有非xml字符串Imports System.IO
Imports System.Xml
Module Module1
Const FILENAME As String = "c:\temp\test.xml"
Sub Main()
'Dim xmlPath As String = Server.MapPath("~/res.xml")
Dim xmlDoc As New XmlDocument
Dim reader As New StreamReader(FILENAME)
Dim xml As String = ""
Dim inputLine As String = ""
Dim foundTag = False
While (Not reader.EndOfStream)
inputLine = reader.ReadLine
If foundTag = False Then
If Not inputLine.StartsWith("<") Then Continue While
foundTag = True
End If
If Not inputLine.StartsWith("--") Then
xml = xml & inputLine
End If
End While
xmlDoc.LoadXml(xml)
End Sub
End Module