反序列化PageSettings时引发的异常 [英] An exception thrown while deserializing PageSettings

查看:104
本文介绍了反序列化PageSettings时引发的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图使用标准的XmlSerializer类将PageSettings对象序列化为XML,但是在反序列化时,它会抛出ArgumentNullException并显示消息 Value不能为空.我对其进行了调查,发现罪魁祸首是PageSettings.PrinterSettings.PrintFileName属性.当所设置的值是null或空字符串时,它的设置器将引发此异常.但是,当该属性的默认值为空字符串时,我应该如何序列化该类,然后反序列化它呢?我是否总是需要为此填充一些有意义的值?

Hi,

I tried to serialize a PageSettings object to XML using the standard XmlSerializer class, but upon deserialization it throws an ArgumentNullException with a message Value cannot be null. I investigated it a bit and found that the culprit is the PageSettings.PrinterSettings.PrintFileName property. It''s setter throws this exception when the value being set is either null or an empty string. But how am I supposed to serialize the class and later deserialize it when the default value of this property is an empty string? Do I always have to fill in some meaningful value for it?

推荐答案

我不知道解决此问题的正式"方法,但是如果没有-一个提出正确答案的方法,您可以尝试以下方法.

在您的序列化方法中,检查PrintFileName,如果它为null或为空,则将其设置为"none"或"empty"或类似的文本.然后正常序列化.

在反序列化方法中,通常对序列进行反序列化,然后检查PrintFileName,如果它等于用于序列化的值,则将其设置为空字符串.

我希望有人能为您提供真正的解决方案.

祝你好运. :)
I don''t know an ''official'' way to resolve this problem but if no-one comes up with the correct answer you could try the following.

In your serialization method, check PrintFileName and if it is null or empty set it to ''none'' or ''empty'' or a similar text. Then serialize normally.

In your deserialization method, deserialize normally then check PrintFileName, if it is equal to whatever you used for serialization set it to an empty string.

I hope someone has a real solution for you.

Good luck. :)


这篇关于反序列化PageSettings时引发的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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