难道XML序列化要求将读取属性/写? [英] Does XML Serialization Require Properties to be Read/Write?

查看:122
本文介绍了难道XML序列化要求将读取属性/写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试XML序列化在我的课,但我注意到,当我运行该程序的ID号并没有得到保存。

I'm testing XML Serialization in my class, but i noticed that the ID number didn't get saved when i ran the program.

所以,我一直在寻找各地和修改了一些东西,但没有什么工作,然后我看到,除ID的所有领域既有获取和设置属性。所以我加了一组;物业我的身份证号和噗它的工作。
现在的问题是,它必须设置;并获得;在我的XML序列化的所有属性功能工作?

So i was looking around and modifying a few things but nothing worked, then i saw that all fields except ID had both get and set properties. So i added a set; property to my ID number and poof it worked. The question is, does it have to be set; and get; function on all my properties for XML Serialization to work?

我不希望对象被创建后,要修改的ID号(它自动生成)。

I don't want the ID number to be modified after the object has been created (its automatically generated).

推荐答案

是的,这基本上是XML序列化的限制。从 XML序列化文档

Yes, this is basically a restriction of XML serialization. From the XML Serialization docs:

只有公共属性和字段可以
序列化。属性必须有
公共访问(获取和设置
方法)。如果你必须序列
的非公开数据,使用
BinaryFormatter类而不是XML
序列化。

Only public properties and fields can be serialized. Properties must have public accessors (get and set methods). If you must serialize non-public data, use the BinaryFormatter class rather than XML serialization.

XML序列化是不够灵活人们可能会喜欢的。

XML serialization isn't as flexible as one might like.

这篇关于难道XML序列化要求将读取属性/写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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