[2005] Serialize any class to Xml and vice-versa-VBForums
Results 1 to 6 of 6

Thread: [2005] Serialize any class to Xml and vice-versa

  1. #1

    Thread Starter
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    533

    [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!

  2. #2
    Addicted Member
    Join Date
    Sep 2008
    Location
    Jacksonville, Florida
    Posts
    147

    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?

  3. #3

    Thread Starter
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    533

    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 09:56 AM.
    If your problem is solved, please use the Mark Thread As Resolved under Thread Tools!

    Show Appreciation. Rate Posts!

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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.
    ---My Flickr photo (mostly screenshots these days!) stream. Have a look!

    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    TCP client/server connection | Retrieving the EventHandler for any Event by code.
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    Addicted Member
    Join Date
    Sep 2008
    Location
    Jacksonville, Florida
    Posts
    147

    Re: [2005] Serialize any class to Xml and vice-versa

    This is a codebank forum. it's for posting code.
    vb.net and C# in 2008 .net 3.5
    ImaginaryDevelopment blog

  6. #6

    Thread Starter
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    533

    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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.