如何替换xml文档中的HEX字符?

发布于 2024-09-04 03:37:33 字数 820 浏览 3 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提笔落墨 2024-09-11 03:37:33

好的,首先您的读取文件的代码已损坏。它通常会起作用,但您应该永远忽略来自Stream.Read的返回值。您还应该使用 Using 语句或 Finally 块关闭流。幸运的是,有一种非常简单的方法可以替换您的代码:

Dim xmlString As String = File.ReadAllText(FileName)
Doc.LoadXml(xmlString)

另一方面,您声称编码并不总是UTF-8 - 那么为什么您总是尝试使用UTF-8?如果您将其作为纯字节加载,实际上会更好:

Dim bytes As Byte() = File.ReadAllBytes(FileName)
Using stream As MemoryStream = new MemoryStream(bytes)
    Doc.Load(stream)
End Using

或者更容易:

Doc.Load(FileName)

现在,如果您这样做,您仍然会遇到相同的错误吗?如果是这样,则文件本身已损坏...

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 a Using statement or Finally block. Fortunately, there's an incredibly easy way of replacing your code:

Dim xmlString As String = File.ReadAllText(FileName)
Doc.LoadXml(xmlString)

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:

Dim bytes As Byte() = File.ReadAllBytes(FileName)
Using stream As MemoryStream = new MemoryStream(bytes)
    Doc.Load(stream)
End Using

or more easily:

Doc.Load(FileName)

Now, if you do that do you still get the same error? If so, the file itself is broken...

提笔落墨 2024-09-11 03:37:33

如果您有无效的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文