包含无效字符的对象的XML序列化 [英] XML Serialization of an Object Containing invalid chars

查看:44
本文介绍了包含无效字符的对象的XML序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在序列化一个字符串属性中包含HTML数据的对象.

I'm serializing an object that contains HTML data in a String Property.

Dim Formatter As New Xml.Serialization.XmlSerializer(GetType(MyObject))
Dim fs As New FileStream(FilePath, FileMode.Create)
Formatter.Serialize(fs, Ob)
fs.Close()

但是当我将XML读回对象时:

But when I'm reading the XML back to the Object:

Dim Formatter As New Xml.Serialization.XmlSerializer(GetType(MyObject))
Dim fs As New FileStream(FilePath, FileMode.Open)
Dim Ob = CType(Formatter.Deserialize(fs), MyObject)
fs.Close()

我收到此错误:

"'', hexadecimal value 0x14, is an invalid character. Line 395, position 22."

.NET不能避免此类错误,转义无效字符吗?

Shouldn't .NET prevent this kind of error, escaping the invalid characters?

这是怎么回事,我该如何解决?

What's happening here and how can I fix it?

推荐答案

它确实应该在序列化步骤中失败,因为

It should really have failed in the serialize step, because 0x14 is an invalid value for XML. There is no way to escape it, not even with &#x14, since it is excluded as a valid character from the XML model. I am actually surprised that the serializer lets this through, as it makes the serializer a non-conforming one.

是否可以在序列化之前从字符串中删除无效字符?您出于什么目的在HTML中添加了 0x14 ?

Is it possible for you to remove the invalid characters from the string before serializing it? For what purpose do you have an 0x14 in HTML?

或者,您是否可能使用一种编码进行编写,而使用另一种编码进行阅读?

Or, is it possible you are writing with one encoding, and reading with a different one?

这篇关于包含无效字符的对象的XML序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆