Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / July 2003

Tip: Looking for answers? Try searching our database.

Is it possible to serialize a read only property

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mudit - 30 Jul 2003 10:45 GMT
Hi,

I am new to XML Serialization. I am interested in serializing my object to
Xml. I only have to serialize and I needn't worry about deserialization.
As per my understanding, only the properties that have both "get" and "set"
can be serailized. Is that so. Is it also possible to serialize properties
that are read only.

Thanks,
Mudit
Dino Chiesa [MSFT] - 30 Jul 2003 17:06 GMT
By default, no, you cannot serialize readonly properties to XML .

But what do you really want to do?  What's the end goal?  There may be a way
to accomplish what you want.

If you serialize with the BinaryFormatter, you can get all obj properties.
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemRuntimeSerializati
onFormattersBinaryBinaryFormatterClassTopic.asp


Alternatively, staying with the XmlSerializer, you could use a hack where
you provide a public setter method, which does nothing.  In this case the
XML Serialization will work, but de-serialization may not behave the way you
want.

public class MyClass {
 private int _myInt;
 public int MyInt {
    get { return _myInt; }
    set { }  // do nothing!!
 }
}

But understand that in this case, at De-serialize time, your data will
disappear.  The XmlSerializer.Deserialize() method will call your setter,
which will do nothing (by your design) and then the data from the XML file
will NOT be set in the (deserialized) object instance.

Also you will have confusing code that may be hard to use or hard to
maintain later: Users of your class may invoke the setter expecting it to
actually work.

-Dino

> Hi,
>
[quoted text clipped - 6 lines]
> Thanks,
> Mudit
Mudit - 31 Jul 2003 01:30 GMT
Thanks Dino,

My goal is just to get an XML representation of some of the properties of my
object. The XML generated is input to another system. This is just a one-way
process and I do not need to deserialize the object.

Currently I have provided set methods that do nothing, but I am looking for
a better solution. Would writing a new serializer be the only solution in
that case.

Mudit

> By default, no, you cannot serialize readonly properties to XML .
>
> But what do you really want to do?  What's the end goal?  There may be a way
> to accomplish what you want.
>
> If you serialize with the BinaryFormatter, you can get all obj properties.

http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemRuntimeSerializati
onFormattersBinaryBinaryFormatterClassTopic.asp


> Alternatively, staying with the XmlSerializer, you could use a hack where
> you provide a public setter method, which does nothing.  In this case the
[quoted text clipped - 31 lines]
> > Thanks,
> > Mudit
Oleg Tkachenko - 31 Jul 2003 09:30 GMT
> My goal is just to get an XML representation of some of the properties of my
> object. The XML generated is input to another system. This is just a one-way
[quoted text clipped - 3 lines]
> a better solution. Would writing a new serializer be the only solution in
> that case.

Another interesting solution could be using ObjectXPathNavigator [1] to
navigate your object and serialize it using XSLT (you can also add any
arbitrary serialization logic there).

[1] http://msdn.microsoft.com/library/en-us/dnexxml/html/xml03172003.asp
Signature

Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Mudit - 31 Jul 2003 10:38 GMT
Thanks,

Thats a really cool feature. I didn't know something like this existed.

Mudit

> > My goal is just to get an XML representation of some of the properties of my
> > object. The XML generated is input to another system. This is just a one-way
[quoted text clipped - 9 lines]
>
> [1] http://msdn.microsoft.com/library/en-us/dnexxml/html/xml03172003.asp

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.