如何替换xml文档中的HEX字符?
我正在尝试将 xml 文件导入 vb.net XmlDocument 但收到错误:
'.',十六进制值 0x00,是无效字符。第 94 行,位置 1。
我想知道是否有办法替换十六进制字符 0x00
以下是第 92,93,94 行,文件在第 94 行结束,
92 | </request>
93 |</rpc> <!-- XML Finishes here -->
94 |
感谢您的帮助。
编辑:添加用于获取文件的代码。
Dim fs As FileStream = File.Open(FileName, FileMode.Open, FileAccess.Read)
Dim buffer(fs.Length) As Byte
fs.Read(buffer, 0, fs.Length)
Dim xmlString As String = System.Text.UTF8Encoding.UTF8.GetString(buffer)
fs.close()
Doc.LoadXml(xmlString.Trim)
我使用 System.Text.UTF8Encoding.UTF8.GetString(buffer) 因为文件编码并不总是 UTF-8。不幸的是,我无法控制 xml 文件,因为我们从外部源接收它,外部源不会更改文件的生成方式,因为它被其他人使用。
我想做的基本上是将文件放入字符串中,然后从最后一个 > > 中切掉它的末尾。然后附加我自己的>或者只是用空字符串替换十六进制字符。
I'm trying to import an xml file into vb.net XmlDocument but am getting the error:
'.', hexadecimal value 0x00, is an invalid character. Line 94, position 1.
I'm wondering if there is a way to replace the hex character 0x00
The following are lines 92,93,94 the file ends on line 94
92 | </request>
93 |</rpc> <!-- XML Finishes here -->
94 |
Thanks for any help.
EDIT: adding code used to get the file.
Dim fs As FileStream = File.Open(FileName, FileMode.Open, FileAccess.Read)
Dim buffer(fs.Length) As Byte
fs.Read(buffer, 0, fs.Length)
Dim xmlString As String = System.Text.UTF8Encoding.UTF8.GetString(buffer)
fs.close()
Doc.LoadXml(xmlString.Trim)
I am using System.Text.UTF8Encoding.UTF8.GetString(buffer) because the file encoding is not always UTF-8. Unfortunatly I don't have controll over the xml file as we are receiving it from an external source who wont change the way the file is generated as it is used by others.
What I want to do is basically get the file into the string then either chop off the end of it from the last > and then append my own > or just replace the HEX character with an empty string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,首先您的读取文件的代码已损坏。它通常会起作用,但您应该永远忽略来自
Stream.Read
的返回值。您还应该使用Using
语句或Finally
块关闭流。幸运的是,有一种非常简单的方法可以替换您的代码:另一方面,您声称编码并不总是UTF-8 - 那么为什么您总是尝试使用UTF-8?如果您将其作为纯字节加载,实际上会更好:
或者更容易:
现在,如果您这样做,您仍然会遇到相同的错误吗?如果是这样,则文件本身已损坏...
Okay, to start with your code for reading a file is broken. It will usually work, but you should pretty much never ignore the return value from
Stream.Read
. You should also close streams using aUsing
statement orFinally
block. Fortunately, there's an incredibly easy way of replacing your code:On the other hand, you claim that the encoding isn't always UTF-8 - so why are you always trying to use UTF-8? It would actually be better if you loaded it as plain bytes:
or more easily:
Now, if you do that do you still get the same error? If so, the file itself is broken...
如果您有无效的 XML,则必须先将其作为常规二进制文件进行更正,然后再将其解析为 XML 文档。
If you have invalid XML, then you'll have to correct it as a regular binary file before parsing it as an XML document.