在 XML 文件中保存转义字符 0x1b
我们有一个使用打印机内部字体与打印机进行通信的应用程序。这需要使用字体描述属性以二进制格式发送一些数据。
要发送的字符之一是转义字符 (0x1b)。系统的状态和所有更改的选项稍后都会保存在 XML 文件中,但是当我们尝试写入值时,我们会得到以下异常:
System.InvalidOperationException: There is an error in XML document (1009, 26). ---> System.Xml.XmlException: '[Some small backwards arrow symbol]', hexadecimal value 0x1B, is an invalid character.
我不太确定为什么这个箭头充当转义符,但它适用于打印机。当我们尝试将其保存在 XML 文件中时会发生错误。有什么建议吗?
We have an application that communicates with printers using their internal printer fonts. This requires sending some data in a binary format using a font description property.
One of the characters to be sent is an escape character (0x1b). The state of the system and all options changed are later saved in an XML file, but when we try to write the value, we get the following exception:
System.InvalidOperationException: There is an error in XML document (1009, 26). ---> System.Xml.XmlException: '[Some small backwards arrow symbol]', hexadecimal value 0x1B, is an invalid character.
I'm not really sure why this arrow functions as escape, but it works on the printer. The error occurs when we try to save it in an XML file. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
i类似问题:
如何在 xml 中转义 unicode 字符 0x1F?
所以要么不推荐的方法
要么使用达林答案中提到的base-64
iSimilar question to:
How do I escape unicode character 0x1F in xml?
So either the not recommended approach of
or using base-64 as mentioned in Darin's answer
还有另一种方法可以使用 System.Xml 功能来转义此类字符。
您只需将 XmlWriterSettings 的 CheckCharacters 标志设置为 false。
但如果您想读取 xml,则必须将 XmlReaderSettings 的相同标志设置为 false。 :)
There is another way to escape such characters by using System.Xml features.
You just need to set CheckCharacters flag to false for XmlWriterSettings.
But you have to set the same flag to false for XmlReaderSettings if you want to read the xml. :)
不可能将此类值保存在 XML 文件中。您可能需要事先对其进行编码。您可以使用 Base 64 来实现此目的。
It is impossible to save such values in an XML file. You might need to encode it before hand. You could use Base 64 for this.
也许您可以用您选择的特殊字符串替换 ESC 字符,例如“MyEsc123”。在写入文件之前,您将所有 0x1B 实例替换为新字符串,并且当您读取文件时,您将再次进行反向转换。
Maybe you could repalce the ESC char with a special string of your choosing, say "MyEsc123". Before writing the file you replace all instances of 0x1B with the new string and when you read the file you make the conversion again backwords.