Serialization (1): xmlserilize

Source: Internet
Author: User
Http://www.codeproject.com/KB/XML/Serialization_Samples.aspx
If the class implements idictionary, it cannot be serialized. For example, HashTables cannot be serialized.
  • By using XML serialization only public properties and fields can be serialized. If Private Members are to be serialized, other serialization Methods shocould be used.
  • It requiresParameterlessConstructor. This constructor is called during deserialization.
  • Serialized form of the object does not contain any type, assembly information of the class. Only the data is stored.
  • Using XML serialization not all the classes can be serialized. classes that implementIDictionaryCannot be serialized. E. g.HashtableS cannot be serialized. arrays of objects can be easily serialized. Collections can be serialized but to serialize collections certain rules must be followed.
  • NULL values are not serialized with XML serialization. To include the null members in the serialized formIsNullableAttribute shocould be settrue. For example:
  • [Xmlelement (isnullable = true)]
    Remove xmlns:

    Xmlserializernamespaces namespaces = new xmlserializernamespaces ();
    Namespaces. Add (string. Empty, String. Empty );
    Serializer. serialize (writer, source, namespaces );

    Change the root element name: rootname Xmlrootattribute root = new xmlrootattribute ();
    Root. elementname = Name;
    Xmlserializer xs = new xmlserializer (typeof (t), root );

    Format attributes of Nested classes in a serialization class:

    [Xmlarrayitem (type = typeof (netatt), elementname = "item")]
    Public list <netatt> netatts;


    You can also customize xmltextwriter to remove XML declaration. <? XML version = "1.0" encoding = "UTF-8"?>

    Public class specialxmlwriter: xmltextwriter
    {
    Bool m_includestartdocument = true;
    Public specialxmlwriter (textwriter TW, bool includestartdocument)
    : Base (TW)
    {
    M_includestartdocument = includestartdocument;
    }
    Public specialxmlwriter (Stream SW, encoding, bool includestartdocument)
    : Base (SW, null)
    {
    M_includestartdocument = includestartdocument;
    }
    Public specialxmlwriter (string filepath, encoding, bool includestartdocument)
    : Base (filepath, null)
    {
    M_includestartdocument = includestartdocument;
    }
    Public override void writestartdocument ()
    {
    If (m_includestartdocument)
    {
    Base. writestartdocument ();
    }
    }
    }

    The test is as follows:

    Xmlserializer serializer = NULL;
    Specialxmlwriter swriter = NULL;
    Try
    {
    Serializer = new xmlserializer (objxml. GetType ());
    Swriter = new specialxmlwriter (server. mappath ("~ /A. xml "), system. Text. encoding. Default, false );
    If (! Isnamespaces)
    {
    Xmlserializernamespaces xnamesspace = new xmlserializernamespaces ();
    Xnamesspace. Add ("","");
    Serializer. serialize (swriter, objxml, xnamesspace );
    }
    Else
    Serializer. serialize (swriter, objxml );
    }
    Catch (exception ex)
    {
    Throw new exception (ex. tostring ());
    }
    Finally
    {
    If (swriter! = NULL)
    {
    Swriter. Close ();
    }
    }

    // Using system;
    Using system. Collections. Generic;
    Using system. LINQ;
    Using system. Web;
    Using system. Web. UI;
    Using system. Web. UI. webcontrols;
    Using system. xml. serialization;
    Using system. IO;
    Using system. runtime. serialization;
    Using system. xml;
    Public partial class default2: system. Web. UI. Page
    {
    Protected void page_load (Object sender, eventargs E)
    {

    }
    Protected void button#click (Object sender, eventargs E)
    {
    List <garyperson> objlist = new list <garyperson> (){
    New garyperson {strpassword = "James", strusername = "p "}
    };
    Serialize (objlist, server. mappath ("~ /Obj. xml "));

    }
    Void serialize (Object source, string file)
    {
    Xmlwritersettings settings = new xmlwritersettings ();
    Settings. omitxmldeclaration = true;
    Settings. indent = true;

    Using (xmlwriter writer = xmlwriter. Create (file, settings ))
    {
    Xmlrootattribute root = new xmlrootattribute ("garylist ");
    Xmlserializer serializer = new xmlserializer (source. GetType (), root );

    Xmlserializernamespaces namespaces = new xmlserializernamespaces ();
    Namespaces. Add (string. Empty, String. Empty );

    Serializer. serialize (writer, source, namespaces );
    }
    }

    }
    [Serializable]
    Public class garyperson
    {
    Public String strusername;
    Public String strpassword;
    }

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.