This message was discovered on microsoft.public.dotnet.framework.sdk.
| Sergiy Mesropyan |
| GOOD ANSWER |
Hi All,
..NET gurus help! :)
I have a class library. I need to add XML serialization to it. I DO NOT want to have default public constructor as it will ruin the library idealogy. How can I use standar (XML) serialization provided by .NET and do not have default public constructor?
Things I tried:
1. Used attribute [Serializable] on the class. 2. Implemented ISerializable interface. 3. Implemented public constructor(SerializationInfo info, StreamingContext context) 4 1&2 5 2 &3
Nothing helps :(
Any ideas?
Thanks you for your help, Sergiy Mesropyan.
|
|
|
| |
|
|
| |
| |
| David Schmidt |
| GOOD ANSWER |
In case 3, it should be a private constructor. When you say nothing helps, what do you mean? Does it crash? Does it not compile?
Implementing the serialization is fairly straight forward. Post a little more about what problems you are having.
-Dave
"Sergiy Mesropyan" <Click here to reveal e-mail address> wrote in message news:edDLGHl2BHA.2216@tkmsftngp04... [Original message clipped]
|
|
|
| |
|
|
| |
| |
| Sergiy Mesropyan |
| GOOD ANSWER |
Well private constructor does not do any good as reflection cannot access it to instantiate the object. Though I tried it anyway with the same result. Here is the test program I use to find the solution. (After your message so It will not clough communication :)
"David Schmidt" <Click here to reveal e-mail address> wrote in message news:#GRUJQm2BHA.1228@tkmsftngp04... [Original message clipped]
using System;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.IO;
namespace ConsoleTester
{
[Serializable]
public class SerializerTest
{
public SerializerTest(string data)
{
this.data = data;
}
private SerializerTest(SerializationInfo info, StreamingContext context)
{
}
public string data;
}
class ConsoleMain
{
[STAThread]
static void Main(string[] args)
{
SerializerTest testInstance = new SerializerTest("Hello World!");
try
{
XmlSerializer ser = new XmlSerializer(typeof(SerializerTest));
TextWriter writer = new StreamWriter("C:\\test.xml");
ser.Serialize(writer, testInstance);
writer.Close();
}
catch(Exception e)
{
//The intresting message is inside inner exception.
Console.WriteLine(e.InnerException.Message);
Console.ReadLine();
}
}
}
}
|
|
|
| |
|
|
| |
| |
| Albert Cornejo |
| GOOD ANSWER |
I use Protected. This also enablels any sub classes to call:
MyBase.New(etc, etc)
"Sergiy Mesropyan" <Click here to reveal e-mail address> wrote in message news:O07brum2BHA.1260@tkmsftngp04... > Well private constructor does not do any good as reflection cannot access it [Original message clipped]
|
|
|
| |
|
|
| |
| |
| Sergiy Mesropyan |
| GOOD ANSWER |
But the class still requires default public constructor!!! "Albert Cornejo" <Click here to reveal e-mail address> wrote in message news:ev4iWzm2BHA.1324@tkmsftngp03... [Original message clipped]
|
|
|
| |
|
|
| |
|
|
|
|
| |
| Albert Cornejo |
| GOOD ANSWER |
The things you tried don't affect Xml Serialization at all. The [Serializable()] attribute is only used by the BinaryFormatter, SoapFormatter or any custom Formatter you may have built.
The fact of the matter is that the XmlSerialization.Deserialize() method needs a constructor to rebuild the object and it requires it be the default public constructor. If you are willing use the BinaryFormatter or SoapFormatter then you wont have this problem.
I wonder why you can't have the default public constructor and not use it? Here is a little trick the hides any method from intellisense:
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsable State.Never)> _ Public Sub New() End Sub
Keep in mind that a client can still use this constructor; it's just not browsable by intellisense or the object browser.
"Sergiy Mesropyan" <Click here to reveal e-mail address> wrote in message news:edDLGHl2BHA.2216@tkmsftngp04... [Original message clipped]
|
|
|
| |
|
|
| |
| |
| Sergiy Mesropyan |
| GOOD ANSWER |
"Albert Cornejo" <Click here to reveal e-mail address> wrote in message news:OVx0lfm2BHA.1904@tkmsftngp02... [Original message clipped]
[Original message clipped]
library not to use public constructor. :) Bellow is the test progran I use:
[Serializable]
public class SerializerTest
{
public SerializerTest()
{
}
public SerializerTest(string data)
{
this.data = data;
}
private SerializerTest(SerializationInfo info, StreamingContext context)
{
}
public string data;
}
class ConsoleMain
{
[STAThread]
static void Main(string[] args)
{
SerializerTest testInstance = new SerializerTest("Hello World!");
try
{
XmlSerializer ser = new XmlSerializer(typeof(SerializerTest));
TextWriter writer = new StreamWriter("C:\\test.xml");
ser.Serialize(writer, testInstance);
writer.Close();
}
catch(Exception e)
{
//The intresting message is inside inner exception.
Console.WriteLine(e.InnerException.Message);
Console.ReadLine();
}
}
}
}
|
|
|
| |
|
|
| |
| |
| Christoph |
| GOOD ANSWER |
[Original message clipped]
The way to customize the output of the XmlSerializer is to attach Metadata attributes, either in you code or at runtime passed to the XmlSerializer. You can find a list of the available attributes in the Framework documentation.
Either way, you're not getting around having a public constructor if you want to use the XmlSerializer.
HTH, Christoph
|
|
|
| |
|
|
| |
|
| |
| Albert Cornejo |
| GOOD ANSWER |
Ok,
There are two main types of serialization: Xml Serialization and regular Serialization
Xml Serialization All of the helper classes to perform Xml Serialization are found in the System.Xml.Serialization namespace. There is one class found in this namespace that serializes and deserializes objects (XmlSerializer). It's the one that you're using. You use XmlSerializer.Serialize and XmlDeserialize. The XmlSerializer only serializes public read/write fields and public read/write properties and it requires that you have a default public constructor to rebuild the object. You don't have to add any attribute tags to perform Xml Serialization. However you may use any of the attribute classes found in the System.Xml.Serialization namespace to customize the serialization. For instance you may not want a public field to get serialized so you would precede it with the tag [XmlIgnore()]. All of the attribute tags that help customize Xml Serialization are found in System.Xml.Serialization.
Regular Serialization All of the helper classes to perform regular Serialization are found in the System.Runtime.Serialization namespace. There are two built in formatters used for regular Serialization: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter System.Runtime.Serialization.Formatters.Soap.SoapFormatter
They each have a Serialize and Deserialize method. If you want to serialize a class using either of these formatters you must precede the class with the [Serializable] attribute. By default, these formatters serialize every member in the class. If want to control what gets serialized you must implement the ISerializable interface. However, implementing this interface is not necessary.
Does this help?
"Sergiy Mesropyan" <Click here to reveal e-mail address> wrote in message news:es#T9xm2BHA.1372@tkmsftngp02... [Original message clipped]
|
|
|
| |
|
|
| |
| |
| Sergiy Mesropyan |
| GOOD ANSWER |
"Albert Cornejo" <Click here to reveal e-mail address> wrote in message news:eJp4jQn2BHA.2048@tkmsftngp05... [Original message clipped]
> Does this help? No.
My question, againe, is : How can I use .NET serialization for classes without default public constructor. I understand that I can write it myself but I want to leverage all the Serialization code that already exists in .NET.
[Original message clipped]
|
|
|
| |
|
|
| |
| |
| Albert Cornejo |
| GOOD ANSWER |
I thought it was clear from the last message.
YOU CAN'T DO IT WITH THE XMLSERIALIZER!!!
YOU HAVE TO EITHER USE THE BINARYFORMATTER OR THE SOAPFORMATTER.
TAKE FIVE MINUTES TO READ HOW THESE CLASSES ARE USED AND YOU'LL SEE THAT IT'S QUITE EASY. YOU WONT NEED A DEFAULT PUBLIC CONSTRUCTOR WITH THESE CLASSES AND YOU WONT HAVE TO IMPLEMENT ANYTHING.
"Sergiy Mesropyan" <Click here to reveal e-mail address> wrote in message news:uaLtySp2BHA.2524@tkmsftngp07... [Original message clipped]
|
|
|
| |
|
|
| |
| |
| Sergiy Mesropyan |
| GOOD ANSWER |
That was the answer I was looking for! Thanks.
PS. If you reread original post you will see that XML serialization is in parentheses as optional. I was looking for ANY kind of serialization. I tried it with binary formatter and it works! Thanks again.
"Albert Cornejo" <Click here to reveal e-mail address> wrote in message news:OsZh0kp2BHA.2460@tkmsftngp07... [Original message clipped]
|
|
|
| |
|
|
| |
|
|
|
|
|
|
|
|
|
|