IT Staff

Blog về chuyên ngành IT

[ C# ] Using a XMLSerializer to read data from XML

leave a comment »

When I’m working on the client side, sometime I send some requests to the service, and the serivce response the result that in xml format. So, I must deserialize this data into a specified object in my client app. The xml file has following structure:









  <SoftwareName>Minze Buchhaltung Lite</SoftwareName>





































In my data, I have a root node <Lizenz> which has some child-nodes as <KundePC>,<CustomerName>,… and a child-node-list as <ListeFunktions>, which has many <Funktion> child-node . The question is how to convert all that data into a specified object.The easy way is using XmlSerializer.

I define a class Lizenz and Funktion with appropriated properties in my apps.

    [XmlTypeAttribute(AnonymousType = true)]

    public class Lizenz


        public Lizenz()


            ListeFunktions = new List<Funktion>();


        [XmlElement(ElementName = “KundePC”)]

        public string KundePC { get; set; }


        [XmlElement(ElementName = “CustomerName”)]

        public string CustomerName { get; set; }


        [XmlElement(ElementName = “SteuerAnzahl”)]

        public string SteuerAnzahl { get; set; }


        [XmlElement(ElementName = “DatumAktiv”)]

        public DateTime DatumAktiv { get; set; }


        [XmlElement(ElementName = “DatumSchaffenSchlüssel”)]

        public DateTime DatumSchaffenSchlüssel { get; set; }


        [XmlElement(ElementName = “Schlüssel”)]

        public string Schlüssel { get; set; }


        [XmlElement(ElementName = “LizenzTyp”)]

        public string LizenzTyp { get; set; }


        [XmlElement(ElementName = “SoftwareName”)]

        public string SoftwareName { get; set; }


        [XmlElement(ElementName = “Begrenzen”)]

        public int Begrenzen { get; set; }


        [XmlElement(ElementName = “MengeMaschine”)]

        public int MengeMaschine { get; set; }


        [XmlElement(ElementName = “MengeBenutzer”)]

        public int MengeBenutzer { get; set; }


        [XmlElement(ElementName = “MengeDatenbank”)]

        public int MengeDatenbank { get; set; }


        [XmlArray(ElementName = “ListeFunktions”)]

        [XmlArrayItem(ElementName = “Funktion”)]

        public List<Funktion> ListeFunktions { get; set; }


        [XmlElement(ElementName = “AktivSchlüssel”)]

        public string AktivSchlüssel { get; set; }


    public class Funktion


        [XmlElement(ElementName = “Menge”)]

        public int Menge { get; set; }


        [XmlElement(ElementName = “Code”)]

        public string Code { get; set; }


Then, deserialize this XML document:

    class Program


        static void Main(string[] args)


            string xml = @”<Lizenz><KundePC>178BFBFF00100F62</KundePC><CustomerName>abc</CustomerName><SteuerAnzahl>1234</SteuerAnzahl><DatumAktiv>2012-03-21T15:25:37</DatumAktiv><DatumSchaffenSchlüssel>2012-07-23T15:25:37</DatumSchaffenSchlüssel><Schlüssel>btahe53e-d345-4ahb-qfgy-34hsdry45sj2</Schlüssel><LizenzTyp>MA</LizenzTyp><SoftwareName>Minze Buchhaltung Lite</SoftwareName><Begrenzen>360</Begrenzen><MengeMaschine>1</MengeMaschine><MengeBenutzer>0</MengeBenutzer><MengeDatenbank>1</MengeDatenbank><ListeFunktions><Funktion><Code>C81B7A6</Code><Menge>0</Menge></Funktion><Funktion><Code>AE043575</Code><Menge>0</Menge></Funktion><Funktion><Code>195557BF</Code><Menge>0</Menge></Funktion><Funktion><Code>F9379464</Code><Menge>0</Menge></Funktion><Funktion><Code>ECE17193</Code><Menge>30</Menge></Funktion><Funktion><Code>F2CB383A</Code><Menge>50</Menge></Funktion><Funktion><Code>2A13879C</Code><Menge>0</Menge></Funktion></ListeFunktions><AktivSchlüssel>+u6ndevWtgl/V0rgHAw3N9EW3ShIqZujRG4Od5i/uBxSzVtmNiL7DA==qWglwJBJMuw</AktivSchlüssel></Lizenz>”;

            var stringReader = new StringReader(xml);

            //FileStream fs = new FileStream(@”e:\temp\license_object.xml”, FileMode.Open);

            var xmlSer = new System.Xml.Serialization.XmlSerializer(typeof(Lizenz)

                , new System.Xml.Serialization.XmlRootAttribute(“Lizenz”));

            object result = xmlSer.Deserialize(stringReader);






Written by Xavier

Tháng Tám 3, 2013 lúc 9:47 sáng

Posted in Staff

Trả lời

Mời bạn điền thông tin vào ô dưới đây hoặc kích vào một biểu tượng để đăng nhập: Logo

Bạn đang bình luận bằng tài khoản Đăng xuất /  Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Đăng xuất /  Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Đăng xuất /  Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Đăng xuất /  Thay đổi )


Connecting to %s

%d bloggers like this: