Set XML tag name for Serialization : Serialization « File Stream « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » File Stream » SerializationScreenshots 
Set XML tag name for Serialization
 
using System;
using System.IO;
using System.Xml.Serialization;

public class Serializer {

  public static void Main(string [] args) {
    Personnel personnel = CreatePersonnel();
    XmlSerializer serializer = new XmlSerializer(typeof(Personnel));
    using (FileStream stream = File.OpenWrite("Employees.xml")) {
      serializer.Serialize(stream, personnel);
    }
  }
  
  private static Personnel CreatePersonnel() {
    Personnel personnel = new Personnel();
    personnel.Employees = new Employee [] {new Employee()};
    personnel.Employees[0].FirstName = "Joe";
    personnel.Employees[0].MiddleInitial = "M";
    personnel.Employees[0].LastName = "Lee";
    
    personnel.Employees[0].Addresses = new Address [] {new Address()};
    personnel.Employees[0].Addresses[0].AddressType = AddressType.Home;
    personnel.Employees[0].Addresses[0].Street = new string [] {"999 Colluden"};
    personnel.Employees[0].Addresses[0].City = "Vancouver";
    personnel.Employees[0].Addresses[0].State = State.BC;
    personnel.Employees[0].Addresses[0].Zip = "V5V 4X7";
    
    personnel.Employees[0].HireDate = new DateTime(2001,1,1);
    
    return personnel;
  }
}


[Serializable]
public enum AddressType {
  Home,
  Office
}

[Serializable]
public enum State {
  [XmlEnum(Name="British C")]BC,
  [XmlEnum(Name="Sask")]SK
}

[Serializable]
public class Address {
  [XmlAttribute(AttributeName="type")]  public AddressType AddressType;
  [XmlElement(ElementName="street")]    public string[] Street;
  [XmlElement(ElementName="city")]      public string City;
  [XmlElement(ElementName="state")]     public State State;
  [XmlElement(ElementName="zip")]       public string Zip;
}

[Serializable]
public class TelephoneNumber {
  [XmlAttribute(AttributeName="type")] public AddressType AddressType;
  [XmlElement(ElementName="areacode")] public string AreaCode;
  [XmlElement(ElementName="exchange")] public string Exchange;
  [XmlElement(ElementName="number")]   public string Number;
}

[Serializable]
public class Employee {
  [XmlAttribute(AttributeName="firstname")]      public string FirstName;
  [XmlAttribute(AttributeName="middleinitial")]  public string MiddleInitial;
  [XmlAttribute(AttributeName="lastname")]       public string LastName;
  
  [XmlArray(ElementName="addresses")]
  [XmlArrayItem(ElementName="address")]      public Address [] Addresses;
  [XmlArray(ElementName="telephones")] 
  [XmlArrayItem(ElementName="telephone")]    public TelephoneNumber [] TelephoneNumbers;
  
  [XmlAttribute(AttributeName="hiredate")]   public DateTime HireDate;
}

[Serializable]
[XmlRoot(ElementName="personnel")]
public class Personnel {
  [XmlElement(ElementName="employee")]
  public Employee [] Employees;
}


           
         
  
Related examples in the same category
1.Use Serializable attribute to mark a generic class
2.Use Serializable attribute to mark a class
3.Deserialize Object
4.Serialize and DeSerialize
5.Three types of Serialization: Binary, Soap, XML
6.Use XML Serialization with Custom ObjectsUse XML Serialization with Custom Objects
7.Working with the Serializable Attribute
8.NonSerialized attributesNonSerialized attributes
9.Serialize hiearchy classesSerialize hiearchy classes
10.C# Serialization C# Serialization
11.Serial Employee class
12.illustrates binary serializationillustrates binary serialization
13.Deserialize
14.Collection Serialization
15.Serializes an object to binary
16.Deserializes an object from binary
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.