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;
}
}
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;
}
}