使用C#.NET在XML与禁止的字符处理 [英] Dealing with forbidden characters in XML using C# .NET

查看:203
本文介绍了使用C#.NET在XML与禁止的字符处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我序列化到XML对象。目前看来,在其中一个属性值包含的十六进制字符0X1E。我试过设置XmlWriterSettings的编码属性既UTF-16和单code,但我还是得到一个异常抛出:

I have an object that I am serializing to xml. It appears that a value in one of the properties contains the hex character 0x1E. I've tried setting The Encoding property of XmlWriterSettings to both "utf-16" and "unicode" but I still get an exception thrown:

这里是一个错误生成XML文档。 ---> System.InvalidOperationException:有出错生成XML文档。 ---> System.ArgumentException:'',十六进制值0X1E,是无效字符

here was an error generating the XML document. ---> System.InvalidOperationException: There was an error generating the XML document. ---> System.ArgumentException: '', hexadecimal value 0x1E, is an invalid character.

有没有办法让这些字符转换为XML?如果不是,是否有其他的字符会引起问题?

Is there any way to get these characters into the xml? If not, are there other characters that will cause problems?

推荐答案

您可以逃脱然后像你会为HTML。 0X1E是那么刚刚替换字符串的记录分隔符相同的十进制30,&30;它应该没问题。

You can escape then like you would for HTML. 0x1E is the same as decimal 30 so just replace your record separator character with the string, "&30;" and it should be OK.

这篇关于使用C#.NET在XML与禁止的字符处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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