|
-
Dec 23rd, 2005, 01:46 AM
#1
[RESOLVED] XML Serialization question
i have a class that contains a few properties and two methods, SaveToFile and LoadFromFile.
if i serialize the class in SaveToFile using serializer.Serialize(fs, this); is it possible to deserialize in the same way without creating a temporary variable to deserialize to and then setting the properties from the temporary variable?
-
Dec 23rd, 2005, 02:42 AM
#2
Re: XML Serialization question
What do you mean by a temporary variable? Deserialising creates an object of the specified type, so you just use that object.
-
Dec 23rd, 2005, 03:22 AM
#3
Re: XML Serialization question
i have a class like the following testclass:
Code:
class testclass
{
string _tmp1;
string _tmp2;
public string Property1
{
get { return _tmp1; }
set { _tmp1 = value; }
}
public string Property2
{
get { return _tmp2; }
set { _tmp2 = value; }
}
void SaveToFile()
{
FileStream fs = new FileStream("tmp.xml", FileMode.Create, FileAccess.Write);
XmlSerializer xmls = new XmlSerializer(typeof(testclass));
xmls.Serialize(fs, this);
fs.Close();
}
}
i would like to add a method "LoadFromFile" to the class testclass that is basically the reverse of SaveToFile.
i can't just do "this = xmls.Deserialize(fs);". is there a better way of doing this other than doing:
Code:
testclass tmp;
tmp = xmls.Deserialize(fs);
this._tmp1 = tmp.Property1;
this._tmp2 = tmp.Property2;
this method works, but is very time consuming if there is a lot of properties.
-
Dec 23rd, 2005, 06:52 AM
#4
Re: XML Serialization question
I've done a similar thing in the past and I made the Save member an public instance method that returned a bool and the Load member a public static method that returned an instance of the class. You would then do something like this to deserialise a file:
Code:
TestClass myTestClass = TestClass.Load(fileName);
-
Dec 23rd, 2005, 07:09 AM
#5
Re: XML Serialization question
great! thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|