Thoughts from Dan Miser RSS 2.0
 Wednesday, December 27, 2006
Serializing between .NET objects and XML files, and vice versa, is extremely easy, yet flexible and powerful. I'll be blogging a few entries about this topic in the days to come. This entry will set the stage, showing the basics of XML serialization.

Assume you have a (stripped down) class definition like this:

    public class Player

    {

        [XmlElement("jerseyNumber")]

        public int Number { get; set; }

        [XmlElement("playerName")]

        public string Name { get; set; }

    }

And you wanted to read/write an XML file that looked like this:

<Player>

  <jerseyNumber>23</jerseyNumber>

  <playerName>Dan</playerName>

</Player>

The key to making this happen is the XmlSerializer class. This is the class that will convert between public properties and XML elements. You can also decorate your class with simple attributes to coerce your class into generating custom XML (i.e. XML attributes, hide or rename elements, etc.).

Here are a couple of helper methods, and a sample that uses them to show you how all of this comes together.

        static string GetXMLFromObject(object obj)

        {

            XmlSerializer ser = new XmlSerializer(obj.GetType());

 

            StringBuilder sb = new StringBuilder();

            using (XmlTextWriter writer = new XmlTextWriter(new StringWriter(sb)))

            {

                writer.Formatting = Formatting.Indented;

                ser.Serialize(writer, obj);

                return sb.ToString();

            }

        }

 

        static object CreateObjectFromXML(string xml, Type xmlType)

        {

            XmlSerializer ser = new XmlSerializer(xmlType);

            object obj = null;

            using (StringReader reader = new StringReader(xml))

            {

                obj = ser.Deserialize(reader);

            }

            return obj;

        }

 

        static void Main(string[] args)

        {

            Player p = new Player();

            p.Number = 23;

            p.Name = "Dan";

            string xml = GetXMLFromObject(p);

            Console.WriteLine(xml);

            Console.WriteLine();

 

            Player q = (Player)CreateObjectFromXML(xml, typeof(Player));

            Console.WriteLine(q.Number + " --> " + q.Name);

            Console.ReadLine();

        }

Other references:
XML Serialization in the .NET Framework

Wednesday, December 27, 2006 11:05:11 AM (Central Standard Time, UTC-06:00)  #    Comments [1] -
.NET | XML
Tracked by:
http://9nh-information.info/35116610/index.html [Pingback]
http://9nd-information.info/06547899/city-of-boston-filing-business-certificate.... [Pingback]
http://9nj-information.info/66191352/index.html [Pingback]
http://9nf-information.info/51804557/index.html [Pingback]
http://9nq-information.info/05474519/index.html [Pingback]
http://9nn-information.info/49643470/index.html [Pingback]
http://9nl-information.info/54933105/index.html [Pingback]
http://9ns-information.info/16704374/index.html [Pingback]
http://9nd-information.info/24610139/index.html [Pingback]
http://9ng-information.info/58054378/coy-fish-tatoos.html [Pingback]
http://9ni-information.info/46106963/index.html [Pingback]
http://9nr-information.info/65331475/index.html [Pingback]
http://9ql-information.info/82287859/index.html [Pingback]
http://9om-information.info/34106451/how-do-they-test-for-steroid-use-.html [Pingback]
http://9qg-information.info/80075325/index.html [Pingback]
http://9oe-information.info/18862979/computer-i-shop.html [Pingback]
http://9qs-information.info/91028566/index.html [Pingback]
http://9ok-information.info/32702088/index.html [Pingback]
http://9qa-information.info/53886116/corriere-della-sera-di-milano.html [Pingback]
http://9sd-information.info/80092124/index.html [Pingback]
http://9rj-information.info/00610586/index.html [Pingback]
http://9ro-information.info/72815003/index.html [Pingback]
http://9rh-information.info/85326462/index.html [Pingback]
http://9sq-information.info/50463563/index.html [Pingback]
http://9rs-information.info/76689549/index.html [Pingback]
http://9sa-information.info/77791012/media-player-per-mac-os.html [Pingback]
http://9rt-information.info/50408583/index.html [Pingback]
http://9uaet-le-informazioni.info/16006624/asta-tribunale-taranto-it.html [Pingback]
http://9uafo-le-informazioni.info/83046733/timberland-veneto.html [Pingback]
http://9uafb-le-informazioni.info/85404142/index.html [Pingback]
http://9uafb-le-informazioni.info/51027551/nervous-cabaret.html [Pingback]
http://9uafi-le-informazioni.info/81997319/index.html [Pingback]
http://9uafn-le-informazioni.info/61244949/graduatoria-circolo-istituto.html [Pingback]
http://9uaho-le-informazioni.info/29292933/index.html [Pingback]
http://9uagd-le-informazioni.info/25975174/index.html [Pingback]
http://9uagc-le-informazioni.info/49039527/animazione-bambino-roma-natale.html [Pingback]
http://9uahq-le-informazioni.info/41139138/roba-da-ricchi.html [Pingback]
http://9uaho-le-informazioni.info/32733555/index.html [Pingback]
http://9uahl-le-informazioni.info/92589435/index.html [Pingback]
http://9uagn-le-informazioni.info/34111133/index.html [Pingback]
http://9uagt-le-informazioni.info/10877178/transennamento-elenco-prezzo.html [Pingback]
http://9uahi-le-informazioni.info/76364812/index.html [Pingback]
http://9uahn-le-informazioni.info/24502797/macchinario-lavorazione-pane-usato.ht... [Pingback]
Navigation
Archive
<February 2010>
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
28123456
78910111213
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Dan Miser
Sign In
Statistics
Total Posts: 337
This Year: 3
This Month: 0
This Week: 0
Comments: 613
All Content © 2010, Dan Miser
DasBlog theme 'Business' created by Christoph De Baene (delarou)