Presisting an object in database

D

DBC User

Hi All,

I am trying to presist an object in database. So I was searching and
found the one (at the bottom of the message) in the groups and I
followed it and it did create and store the data. But the data is
incomplete. In the following example instead of using struct I used
class. When I say the data is incomplete, I meant the value of the
members are gone. When I store the object in the table, I am storing it
as BLOB.

Here is the class I am trying to presist

public class GSValues
{
private int year;
private User user;

public GSValues()
{
}

public GSValues(int year, User usr)
{
year = yearSelection;
user = usr;
}

public int Year
{
get { return year; }
}
public User CurrentUser
{
get { return user; }
}
}

Following is my serialisation part
XmlSerializer x = new XmlSerializer(typeof(GSValues));
string xmlString;
using (StringWriter sw = new StringWriter())
{
x.Serialize(sw, selectedValues);
xmlString = sw.ToString();
}
System.Text.ASCIIEncoding enc = new ASCIIEncoding();
SelectionObject = enc.GetBytes(xmlString);

Following is deserialisation part

GSValues value = null;
XmlSerializer x = new XmlSerializer(typeof(GSValues));
ASCIIEncoding enc = new ASCIIEncoding();
string xmlString = enc.GetString(SelectionObject);
using (StringReader sr = new StringReader(xmlString))
{
value = (GSValues)x.Deserialize(sr);
}
return value;


Could some one explain to me what am I doing wrong?

Thanks in advance.


----- Message from groups ------
Normally when I approach issues, I take all the crude out of the
picture.
For example, the db processing here has nothing to do with
serialization, so
remove it till you get your serializer working. I am guessing your
input
string is not right. Test it directly with your object like so:

// Serialize.
KeyValue kv = new KeyValue("123", "Hello");
XmlSerializer x = new XmlSerializer(typeof(KeyValue));
string xmlString;
using( StringWriter sw = new StringWriter() )
{
x.Serialize(sw, kv);
xmlString = sw.ToString();



}


// Deserialize.
XmlSerializer ser = new XmlSerializer(typeof(KeyValue));
using (StringReader sr = new StringReader(xmlString))
{
KeyValue kv2 = (KeyValue)ser.Deserialize(sr);
Console.WriteLine("Key:" + kv2.Key);
Console.WriteLine("Value:" + kv2.Value);


}


public struct KeyValue
{
public string Key;
public string Value;

public KeyValue(string key, string value)
{
this.Key = key;
this.Value = value;
}
}
 
S

simida

I think that the best practice is you should use Serializable attribute
on GSValues type and User type. Apply the SerializableAttribute
attribute to a type to indicate that instances of this type can be
serialized, so you can control which type can be serialized or can't be
serialized.

You can also use XSD designer to design your class shema and use XSD
tool to generate class source code. It will add xml serializable
attribute automatically.

Ok, my suggestions is based on the data on the database is complete. If
data is not complete, I think you should inspect the database table
design view and table column setting.

hope useful for you.

Sincerely,
simida
 
M

Markus Stoeger

DBC User wrote:

Hi,
Here is the class I am trying to presist

public class GSValues
{
private int year;
private User user;

Here is the problem. The XmlSerializer only serializes public members.
Your fields are private. Either make them public or add a public setter
to the properties. It should work with XmlSerialization after you do that.

The problem with that is that you have to make all the serializable
members public, giving everyone access to them. You might want to create
small data classes, which are additionally encapsulated in the real
classes that the user gets to see.

There are also object databases. Have you taken a look at some of them?

hth,
Max
 
D

DBC User

Hi Max,

Thanks, after looking my code I realised I do not need to presist the
user field. So I changed all the fields to public and I added
[NonSerialized] attribute to User type declaration now I am getting an
error
"There was an error reflecting field 'user'"

Thanks.
 
M

Marc Gravell

[NonSerialized] applies to fields, i.e. binary serialization; for
property-based xml-serialization you need [XmlIgnore] - I would make the
fields private though, and expose through public getters / setters (needs
both on a serialized property)

Marc
 
D

DBC User

Thanks a lot and it helped. All the documents I read somehow either
missed it or not given that importance.
Again thanks and is now working.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top