Diluksha 的个人资料Diluksha's space照片日志列表更多 工具 帮助

日志


    10月9日

    XML Serialization with .Net

     

    What is XML Serialization?

    Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an object and transport it over the Internet using HTTP between a client and a server. On the other end, deserialization reconstructs the object from the stream.

    And also serialization of XML to common language runtime objects enables one to convert XML documents into a form where they are easier to process using conventional programming languages.

    Sample with C# .Net

    1. Create a console application

    2. Add new XML schema to the project

    clip_image002

    3. Add new Complex type and an Element type to the schema.

    clip_image004

    4. Run following command in Visual Studio Command Prompt to convert created XML schema to the C# class.

    >xsd.exe /c Person.xsd

    This will create a C# file named “Person.cs” inside the solution folder. Add the created C# file into the solution.

    5. xsd.exe is a utility to generate schema or class files from given source. It is included with the .NET Framework. There are three main uses for xsd.exe.

    · xsd.exe <schema>.xsd which will create source code for the appropriate class to de-serialize your object.

    · xsd.exe <assembly>.dll|.exe is used to read the code and generate a schema (.xsd) file for you.

    · xsd.exe <instance>.xml|.xdr which will try to infer an .xsd file which you can then use to create an appropriate class

    6. Code sample to do a serialization

    using System;
    using System.Collections.Generic;
    using System.Text;

    using System.Xml.Serialization;
    using System.IO;

    namespace SerializationSample
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Serializing..
                Person person = new Person();
                person.id = 100;
                person.name = "Diluksha Geeganage";
                person.address = "Sri Lanka";
                person.age = 27;

                XmlSerializer xmlS = new XmlSerializer(typeof(Person));
                FileStream file = new FileStream(@"Persons.xml", FileMode.Create, FileAccess.Write);
                xmlS.Serialize(file, person);

                Console.WriteLine("Serializing Completed");
                Console.Read();

                file.Close();
            }
        }
    }

    7. Code sample to do a deserialization

    using System;
    using System.Collections.Generic;
    using System.Text;

    using System.Xml.Serialization;
    using System.IO;

    namespace SerializationSample
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Deserializing..
                Person objPerson;

                XmlSerializer xmlD = new XmlSerializer(typeof(Person));
                FileStream fileD = new FileStream(@"Persons.xml", FileMode.Open, FileAccess.Read);

                objPerson = (Person)xmlD.Deserialize(fileD);
                Console.WriteLine("ID {0} Name {1} Address {2} Age {3}", objPerson.id, objPerson.name, objPerson.address, objPerson.age);
                Console.Read();

                fileD.Close();
            }
        }
    }

     

     

    评论

    请稍候...
    很抱歉,您输入的评论太长。请缩短您的评论。
    您没有输入任何内容,请重试。
    很抱歉,我们当前无法添加您的评论。请稍后重试。
    若要添加评论,需要您的家长授予您相应权限。请求权限
    您的家长禁用了评论功能。
    很抱歉,我们当前无法删除您的评论。请稍后重试。
    您已超过了一天之内允许提供的评论数上限。请在 24 小时后重试。
    因为我们的系统表明您可能在向其他用户提供垃圾评论,您的帐户已禁用了评论功能。如果您认为我们错误地禁用了您的帐户,请联系 Windows Live 支持部门
    完成下面的安全检查,您提供评论的过程才能完成。
    您在安全检查中键入的字符必须与图片或音频中的字符一致。

    若要添加评论,请使用您的 Windows Live ID 登录(如果您使用过 Hotmail、Messenger 或 Xbox LIVE,您就拥有 Windows Live ID)。登录


    还没有 Windows Live ID 吗?请注册

    引用通告

    此日志的引用通告 URL 是:
    http://diluksha.spaces.live.com/blog/cns!27E84B68CF32B105!292.trak
    引用此项的网络日志