在 XML 文件中保存转义字符 0x1b

发布于 2024-10-01 12:37:12 字数 435 浏览 9 评论 0原文

我们有一个使用打印机内部字体与打印机进行通信的应用程序。这需要使用字体描述属性以二进制格式发送一些数据。

要发送的字符之一是转义字符 (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 技术交流群。

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

发布评论

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

评论(4

挽你眉间 2024-10-08 12:37:12

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

走过海棠暮 2024-10-08 12:37:12

还有另一种方法可以使用 System.Xml 功能来转义此类字符。

您只需将 XmlWriterSettings 的 CheckCharacters 标志设置为 false。

using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings { CheckCharacters = false }))
{
  document.Save(xmlWriter);
}

但如果您想读取 xml,则必须将 XmlReaderSettings 的相同标志设置为 false。 :)

using (XmlReader reader = XmlReader.Create(stringReader, new XmlReaderSettings { CheckCharacters = false }))
{
  document = XDocument.Load(reader);
}

There is another way to escape such characters by using System.Xml features.

You just need to set CheckCharacters flag to false for XmlWriterSettings.

using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings { CheckCharacters = false }))
{
  document.Save(xmlWriter);
}

But you have to set the same flag to false for XmlReaderSettings if you want to read the xml. :)

using (XmlReader reader = XmlReader.Create(stringReader, new XmlReaderSettings { CheckCharacters = false }))
{
  document = XDocument.Load(reader);
}
腹黑女流氓 2024-10-08 12:37:12

不可能将此类值保存在 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.

深海少女心 2024-10-08 12:37:12

也许您可以用您选择的特殊字符串替换 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.

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