XML Serialization with C#

Wanna know a easy way to serialize your class to XML ???
Here we go…

This is the class myXMLSerializer.cs

We gonna need System.IO, System.Text and, of course, System.Xml.Serialization.
using System;
using System.IO;
using System.Data;
using System.Text;
using System.Configuration;
using System.Xml;
using System.Xml.Serialization;

I created a sealed class with one static method to make a common XML Serializer for my app, use as you wish.
namespace how2do.XML
{
    public sealed class myXMLSerializer
    {
      public static XmlDocument SerializeObject(Type type, object objToSerialize)
      {

Now we create a MemoryStream because we don’t want a file stream or whatever we see in a lot of examples over the internet, I just wanted to work with string-memory-XmlDocument… (I was so pissed off because I couldn’t find something similar, people assume we are always using .xml files)
        // Serialization
        MemoryStream mStream = new MemoryStream();

Here, the correct parameter shall be: … = new XmlSerializer(typeof(YourClass)); Ok?
        XmlSerializer objSerializer = new XmlSerializer(type);

I got a lot of problems using UTF8 here (to create the stream) and also using UTF8 converting to string
        //must be Encoding.Default due problems with UTF8Enconding which is used to get the XML as string
        XmlTextWriter xmlWriter = new XmlTextWriter(mStream, Encoding.Default);
        objSerializer.Serialize(xmlWriter, objToSerialize);

Now, taking the damn XML as string. Let’s create our beloved XmlDocument.
        XmlDocument xmlObject = new XmlDocument();

I’m using UTF8, don’t remember why… ah! XmlDocument.LoadXml is a pain in the ass, UFT8 works great =)
        UTF8Encoding encoding = new UTF8Encoding();
        mStream = (MemoryStream)xmlWriter.BaseStream;

The trick
        string strXML = encoding.GetString(mStream.ToArray());

Now loading into the XmlDocument using LoadXml(), not Load() because Load() is for files and files sucks.
        xmlObject.LoadXml(strXML);
        return xmlObject;
      }
    }
}

ABOUT THE CLASSES YOU WILL SERIALIZE

Don’t forget to use the XML Attributes into the classes you want to serialize.

  • using System.Xml.Serialization;
  • A parameterless contructor to your class, OR the class simply WON’T serialize
  • [XmlAttribute] and others Attributes
  • [XmlIgnore] in get/set property where can return a class which one you dont want/cannot create a parameterless constructor

If you have some property like this:
public string Name
{
  get { return this.strName; }
}

You must put a empty set otherwise the XML Serializer won’t get it (your class WILL be serialized but without james:
public string Name
{
  get { return this.strName; }
  set { }
}

Using:
XmlDocument xml = myXMLSerializer.SerializeObject(typeof(yourclass), yourinstance));

=)

98 Responses to “ XML Serialization with C# ”

  1. DARRELL Says:


    Pillspot.org. Canadian Health&Care.Special Internet Prices.No prescription online pharmacy.Best quality drugs. Online Pharmacy. Buy drugs online

    Buy:Prednisolone.Synthroid.Retin-A.Prevacid.Valtrex.Human Growth Hormone.Zovirax.Arimidex.Lumigan.100% Pure Okinawan Coral Calcium.Nexium.Actos.Accutane.Zyban.Petcam (Metacam) Oral Suspension.Mega Hoodia….

  2. CECIL Says:


    Pillspot.org. Canadian Health&Care.No prescription online pharmacy.Special Internet Prices.Best quality drugs. No prescription drugs. Order pills online

    Buy:Arimidex.Zovirax.Prevacid.Zyban.Accutane.Petcam (Metacam) Oral Suspension.Valtrex.Lumigan.Mega Hoodia.Synthroid.Prednisolone.Human Growth Hormone.Retin-A.Nexium.Actos.100% Pure Okinawan Coral Calcium….

  3. Headphone Says:

    boostaroo http://zventlessj3ss.ANTIQUEFURNINISHING.INFO/tag/boostaroo+amp+Headphone/ : amp…

    Headphone…

  4. SENTRA Says:

    NISSAN http://8qd.tjn.ll1.co : SENTRA…

    NISSAN…

  5. Alexander1 Says:

    ANGEL Says:

    SALVADOR Says:

    DUSTIN Says:

    EDUARDO Says:

    BRANDON Says:

    WALLACE Says:

    SALVADOR Says:

    LEWIS Says:

    ENRIQUE Says:

    JIMMIE Says:

    PERRY Says:

    BRADLEY Says:

    RALPH Says:

    SAMUEL Says:

    FELIX Says:

    BRADLEY Says:

    ROLAND Says:

    NATHAN Says:

    ADAM Says:

    LOUIS Says:

    BRUCE Says:

    ANDREW Says:

    ALBERT Says:

    WILLIAM Says:

    RAY Says:

    VICTOR Says:

    SAMUEL Says:

    MARCUS Says:

    LAWRENCE Says:

    DARYL Says:

    MARION Says:

    ALLEN Says:

    JOEL Says:

    JOHNNY Says:

    AUSTIN Says:

    LEROY Says:

    MARION Says:

    JASON Says:

    RONALD Says:

    WENDELL Says:

    LEWIS Says:

    BRUCE Says:

    DUANE Says:

    REGINALD Says:

    GERARD Says:

    EVAN Says:

    < a href=”http://trig.com/advair5771/biography/?ml=Advair-25mcg/50mcg-25mcg/125mcg-25mcg/250mcg-50mcg/500mcg-50mcg/100mcg-50mcg/250mcg-Without-Prescription Advair@25mcg50mcg.25mcg125mcg.25mcg250mcg.50mcg500mcg.50mcg100mcg.50mcg250mcg.Without.Pr

    Buydrugs without prescription ygy…

  6. JON Says:

    JEFF Says:

    WENDELL Says:

    BRYAN Says:

    OLIVER Says:

    DAN Says:

    ERIC Says:

    BRENT Says:

    RONNIE Says:

    FERNANDO Says:

    OLIVER Says:

    MELVIN Says:

    oral cancer hospital

    Buy_generic meds…

  7. PATRICK Says:

    joke male birth control

    Buy_generic meds…

  8. FREDRICK Says:

    prozac daily strength

    Buy_generic pills…

  9. AUSTIN Says:

    does birth control control moods

    Buy_generic drugs…

  10. JOSHUA Says:

    chf vs pulmonary edema

    Buy_generic drugs…

  11. LOUIS Says:

    acne in cats

    Buy_it now…

  12. BRADLEY Says:

    breast cancer spread intestine

    Buy_generic drugs…

  13. DUSTIN Says:

    weight loss tricks for 40 somethings

    Buy_drugs without prescription…

  14. WESLEY Says:

    nursing times alzheimer mavis ford

    Buy_it now…

  15. BILLY Says:

    diseaeses contracted from sand fleas

    Buy_now it…

  16. FRANCIS Says:

    pet ct scan results accuracy cancer

    Buy_drugs without prescription…

  17. NATHAN Says:

    pictures of herpes blisters

    Buy_generic pills…

  18. MARCUS Says:

    hypertension police ma

    Buy_drugs without prescription…

  19. WESLEY Says:

    reversing alcohol induced neuropathy

    Buy_generic meds…

  20. ALBERT Says:

    psychostimulant treatment of depression

    Buy_drugs without prescription…

  21. MITCHELL Says:

    natural cures for premature ejaculation

    Buy_generic drugs…

  22. CLINTON Says:

    copd lung sounds

    Buy_it now…

  23. BRENT Says:

    images of tongue cancer

    Buy_generic meds…

  24. CASEY Says:

    canadian paediatric diabetes association

    Buy_now…

  25. KEN Says:

    discovery of zinc in human health

    Buy_now…

  26. RUBEN Says:

    emotions surviveing uterine cancer

    Buy_generic meds…

  27. VERNON Says:

    azithromycin doxycycline over the counter

    Buy_generic pills…

  28. RICK Says:

    forensic drug test swab

    Buy_no prescription…

  29. LESLIE Says:

    clinical studies of clonidine

    Buy_no prescription…

  30. RYAN Says:

    diet generator reviews

    Buy_it now…

  31. ALEJANDRO Says:

    psychiatric care model

    Buy_generic meds…

  32. MARVIN Says:

    most successful diet ever

    Buy_generic pills…

  33. RUSSELL Says:

    dr loss phil ultimate weight

    Buy_no prescription…

  34. NATHANIEL Says:

    sleep apnea side effect of lexapro

    Buy_generic pills…

  35. JIM Says:

    indomethacin opthalmic drops

    Buy_generic pills…

  36. FERNANDO Says:

    hormone shot for prostate cancer

    Buy_generic drugs…

  37. COREY Says:

    tobacco alternatives herbal

    Buy_generic drugs…

  38. RAMON Says:

    regulations for dementia units

    Buy_drugs without prescription…

  39. JOHNNIE Says:

    black molley slang drug name

    Buy_generic drugs…

  40. HUGH Says:

    JACKIE Says:

    benadryl dogs

    Buy_generic drugs…

Leave a Reply