-
Jun 27th, 2008, 03:40 PM
#1
Thread Starter
Fanatic Member
[2005] Serialize any class to Xml and vice-versa
A few classes cannot be serialized to XML because they do not support ISerializable. The most notable being the Dictionary class. However, if it can't serialize, it will just throw an exception about it.
Code:
using System.Xml;
using System.Xml.Serialization;
using System.IO;
public string Serialize<T>(T entity)
{
StringBuilder outputXml = new StringBuilder();
if (typeof(T).IsSerializable)
{
XmlSerializer ser = new XmlSerializer(typeof(T));
using (TextWriter stream = new StringWriter(outputXml))
{
ser.Serialize(stream, entity);
}
return outputXml.ToString();
}
return null;
}
public T Deserialize<T>(string xml)
{
T entity;
if (typeof(T).IsSerializable)
{
XmlSerializer ser = new XmlSerializer(typeof(T));
using (TextReader stream = new StringReader(xml))
{
entity = (T)ser.Deserialize(stream);
}
return entity;
}
return null;
}
Last edited by MetalKid; May 6th, 2009 at 09:52 AM.
If your problem is solved, please use the Mark Thread As Resolved under Thread Tools!
Show Appreciation. Rate Posts!
-
Oct 9th, 2008, 12:10 PM
#2
Addicted Member
Re: [2005] Serialize any class to Xml and vice-versa
Say there object you use this code on, has a list(of T) as one of it's public properties, will it automatically serialize this object properly or would that require writing something recursive to do the job?
-
Jan 8th, 2009, 10:48 AM
#3
Thread Starter
Fanatic Member
Re: [2005] Serialize any class to Xml and vice-versa
It will automatically serialize all properties/objects recursively underneath it (there may be a max depth, but I don't know what it is), assuming the property isn't marked with an [XmlIgnore] attribute tag in the actual class.
Last edited by MetalKid; Jan 8th, 2009 at 10:56 AM.
If your problem is solved, please use the Mark Thread As Resolved under Thread Tools!
Show Appreciation. Rate Posts!
-
Jan 8th, 2009, 10:56 AM
#4
Re: [2005] Serialize any class to Xml and vice-versa
Im a bit confused with your original post, is this some sort of question or are you just sharing the code you posted?
If it is a question: You can find out if a type is serializable using the IsSerializable property of the Type class.
-
Jan 8th, 2009, 12:06 PM
#5
Addicted Member
Re: [2005] Serialize any class to Xml and vice-versa
This is a codebank forum. it's for posting code.
-
Jan 8th, 2009, 01:07 PM
#6
Thread Starter
Fanatic Member
Re: [2005] Serialize any class to Xml and vice-versa
Yeah, just posting code. The only issue with the code is that some Types cannot be serailized to XML. I think it is because they do not support ISerializable, but I could be wrong. At any rate, if they cannot be serialized, an exception will be thrown alerting you that it cannot be.
If your problem is solved, please use the Mark Thread As Resolved under Thread Tools!
Show Appreciation. Rate Posts!
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
|