I have to use XmlSerializer to serialize a class and I've found big
problems serializing properties and indexers.
I assumed that if you serialize a class with public properties (or an
indexers) containing a field, even private, the serialization process
would serialize that field too assuming it is needed to that public
property. This is not the case ...
For example I want to serialize the following class:
[Serializable]
public class Test
{
readonly int[] a; // same results with private
readonly int b; // same results with private
public Test(int[] v1, int b)
{
a = v1;
b = v2;
}
public Test() : this(new int[] {10, 10}, 50) { }
public int this[int i]
{
get { return a[i]; }
set { a[i] = value; }
}
public bool b100
{
get { return b 100; }
set { }
}
}
I serialize it after constructing it with
Test test = new Test(new int[] { 30, 30 }, 200);
the resulting xml contains:
<test>
<b100>true</b100>
</test>
that is not useful in any way.
If I change the readonly (or private, it is the same) members class to
public the resulting xml contains:
<test>
<a>
<int>30</int>
<int>30</int>
</a>
<b>200</b>
<b100>true</b100>
</test>
That is what I want.
Finally the question: how can I serialize my classes without having to
make all fields public and without using simple expose properties like
public int A
{
get { return a; }
set { a = value; }
}
???
Thank you,
Matteo 6 3793
XmlSerializer works on public properties (not indexers); that is what
it does! You can customise by implementing IXmlSerializable; however,
another (simpler) approach may be to look into DataContractSerializer
(the new WCF serializer). This allows you to specify [DataMember] on
either properties or fields (public or private). The output is very
comparable.
Marc
Working example:
using System;
using System.Runtime.Serialization;
using System.Xml;
using System.Text;
using System.IO;
class Program {
static void Main() {
DataContractSerializer dcs = new
DataContractSerializer(typeof(Test));
Test test = new Test(new int[] { 30, 30, 30, 30 }, 200); // (I
change the array bounds just to check something)
StringBuilder sb = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(sb)) {
dcs.WriteObject(writer, test);
}
string xml = sb.ToString();
using (XmlReader reader = XmlReader.Create(new
StringReader(xml))) {
Test test2 = (Test) dcs.ReadObject(reader);
}
}
}
[DataContract]
public class Test
{
[DataMember(Name="a")]
readonly int[] a; // same results with private
[DataMember]
readonly int b; // same results with private
public Test(int[] v1, int b)
{
a = v1;
this.b = b; // typo by OP
}
<SNIP>: the rest is "as was";
On Feb 28, 1:21 pm, "Marc Gravell" <marc.grav...@gmail.comwrote:
XmlSerializer works on public properties (not indexers); that is what
it does! You can customise by implementing IXmlSerializable; however,
another (simpler) approach may be to look into DataContractSerializer
(the new WCF serializer). This allows you to specify [DataMember] on
either properties or fields (public or private). The output is very
comparable.
Marc
Where is located DataContractSerializer class? Have I to dl from msdn?
But I fear the big problem is another: from the documentation it
appears DataContractSerializer it is not supported by Compact
framework, and the only reason I use XmlSerializer instead of binary
serialization is that compact framework supports only xml
serialization.
Yes, DataContractSerializer is available via MS download: search for
"WCF". But equally, no it doesn't appear to support CF (you didn't
mention you needed this ;-p). Assuming this is correct, then
IXmlSerializable remains an option (supported in CF 1.0 and 2.0);
shouldn't be too tricky...
Marc
On Feb 28, 1:50 pm, "Marc Gravell" <marc.grav...@gmail.comwrote:
Yes, DataContractSerializer is available via MS download: search for
"WCF". But equally, no it doesn't appear to support CF (you didn't
mention you needed this ;-p). Assuming this is correct, then
IXmlSerializable remains an option (supported in CF 1.0 and 2.0);
shouldn't be too tricky...
Marc
Sorry not having mentioned cf.
I think implementing IXmlSerializable takes off all the advantages of
using serialization. I use serialization to exchange a big class of
"parameters" between two application instead of using a customized
properties.txt file. If I have to customize the writing and reading of
a class based on the class structure then all the advantage is
gone ...
Thanks much anyway.
Matteo
One thing I've done before when working with XmlSerializer and complex
objects that don't interact with it well is store all of it's
"private" data in a separate class which is really public, kinda like
a memento. Then when I serialize my class I really just serialize the
memento and same with deserialize.
HTH,
Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On 28 Feb 2007 03:07:25 -0800, "Beorne" <ma*******@gmail.comwrote:
>I have to use XmlSerializer to serialize a class and I've found big problems serializing properties and indexers.
I assumed that if you serialize a class with public properties (or an indexers) containing a field, even private, the serialization process would serialize that field too assuming it is needed to that public property. This is not the case ... For example I want to serialize the following class: This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Peter Cresswell |
last post by:
Hello everyone,
I would like to serialize an object to XML. Currently my code will serialize
all of the public properties that are value types, but not my public
properties that have get...
|
by: Jamus Sprinson |
last post by:
Before I continue, I'm going to begin by saying I'm not by any means
an expert- I've been using .NET with C# for about 4 months now, and
basically just learning by example and docs.
A game...
|
by: Steve Long |
last post by:
Hello,
I hope this is the right group to post this to. I'm trying to serialize a
class I've written and I'd like to be able to serialze to both binary and
xml formats. Binary serialization is...
|
by: Gary Morris |
last post by:
Hello all,
OK, first of all I have known about properties since VB6, which
I have and have used extensively. It seems that property get
and set are basically the same concept in C# and VB.NET,...
|
by: Marcin Grzębski |
last post by:
I red MSDN article of C# 2.0 this week... and i found
very strange syntax for properties e.g.:
public int MyIntValue {
get {
// ...
}
protected set {
// ...
}
| |
by: Phil Galey |
last post by:
I'm using XMLSerializer to serialize hierarchical Class-based objects and
their content to XML. There are a number of fields (i.e. properties) that
are optional, meaning that if no data id written...
|
by: Beorne |
last post by:
I'm serializing a big class using XmlSerializer (I need xml
serialization cause compact framework). I've learn that all my fields
must be public. Ok.
Now it is completly useless to serialize my...
|
by: =?Utf-8?B?ai5hLiBoYXJyaW1hbg==?= |
last post by:
Hi,
I have a schema that has an optional element, fieldTag4000Field. If the
element is omitted from the XML request, when it is deserialized, it will be
null when I check it - which is fine.
...
|
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= |
last post by:
Hi,
I am using XML serialization for the first time and I have noticed
something unexpected. The object I am serializing contains a field
private NumericSettings _numericSettings;
public...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| | |