Specify format and indentation for object XML serialization : Xml serialization : XML C# Examples


C# Examples » XML » Xml serialization »

 

Specify format and indentation for object XML serialization









    
using  System;
using  System.Drawing;
using  System.Collections;
using  System.ComponentModel;
using  System.Data;
using  System.IO;
using  System.Xml;
using  System.Xml.Serialization;

public  class  MainClass
{
    static  void  Main()  
    {
        try
        {
            MemberList  g  =  new  MemberList(  "group  name"  );
            g.members[0]  =  new  Member(  "mem  1"  );
            g.members[1]  =  new  Member(  "mem  2"  );
            g.members[2]  =  new  Member(  "mem  3"  );

            StringWriter  sw  =  new  StringWriter();
            XmlTextWriter  tw  =  new  XmlTextWriter(  sw  );
            tw.Formatting  =  Formatting.Indented;
            tw.Indentation  =  4;

            XmlSerializer  ser  =  new  XmlSerializer(  typeof(  MemberList  )  );
            ser.Serialize(  tw,  g  );

            tw.Close();
            sw.Close();

            Console.WriteLine(sw.ToString());
        }
        catch(  Exception  exc  )
        {
            Console.WriteLine(exc.Message  );
        }

    }
}

public  class  MemberList
{
    public  MemberList()
    {
        members  =  new  Member[5];
    }
    public  MemberList(  string  name  )  :  this()
    {
        m_name  =  name;
    }

    public  string  m_name;
    public  Member[]  members;
}

public  class  Member
{
    public  Member()
    {
    }
    public  Member(  string  name  )  :this()
    {
        m_name  =  name;
    }

    public  string  m_name;
}
    
   
  
   



Output

group namemem 1mem 2mem 3


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo XML
» Xml serialization