Serialize to SOAP based XML file : Serialization SOAP « 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 » Serialization SOAPScreenshots 
Serialize to SOAP based XML file
Serialize to SOAP based XML file

    using System;
  using System.IO;
  using System.Runtime.Serialization.Formatters.Binary;
  using System.Runtime.Serialization.Formatters.Soap;
  

    public class RoomApp
    {
    public static void Main()
    {
      // Make a room and listen to the tunes.
      Console.WriteLine("Made a My Room...");
      MyRoom myAuto = new MyRoom("My"50, false, true);
      myAuto.TurnOnRadio(true);
      myAuto.GoUnderWater();

      // Save the same room into SOAP format.
      Console.WriteLine("Now saving room to XML file");
      FileStream myStream = File.Create("RoomData.xml");
      SoapFormatter myXMLFormat = new SoapFormatter();
      myXMLFormat.Serialize(myStream, myAuto);
      myStream.Close();

      // Read in the Room from the XML file.
      Console.WriteLine("Reading room from XML file.");
      myStream = File.OpenRead("RoomData.xml");
      MyRoom roomFromXML = (MyRoom)myXMLFormat.Deserialize(myStream);
      Console.WriteLine(roomFromXML.PetName + " is alive!");
      roomFromXML.TurnOnRadio(true);
      myStream.Close();
      
    }
    }  
  
  
  [Serializable]
    public class Radio
    {
    [NonSerialized]
    private int objectIDNumber = 9;

        public Radio(){}
    public void On(bool state)
    {
      if(state == true)
        Console.WriteLine("Music is on...");
      else
        Console.WriteLine("No tunes...");        
    }
    }


  [Serializable]
    public class Room
    {
    protected string petName;
    protected int maxInternetSpeed;
    protected Radio theRadio = new Radio();

        public Room(string petName, int maxInternetSpeed)
        {
      this.petName = petName;
      this.maxInternetSpeed = maxInternetSpeed;
        }
    public Room() {}

    public String PetName
    {
      get return petName; }
      set petName = value; }
    }
    public int MaxInternetSpeed
    {
      get return maxInternetSpeed; }
      set maxInternetSpeed = value; }
    }

    public void TurnOnRadio(bool state)
    {
      theRadio.On(state);
    }
    }


  [Serializable]
    public class MyRoom : Room
    {
    protected bool isFlightWorthy;
    protected bool isSeaWorthy;

    public MyRoom(string petName, int maxInternetSpeed, 
              bool canFly, bool canSubmerge)
      : base(petName, maxInternetSpeed)
        {
      this.isFlightWorthy = canFly;
      this.isSeaWorthy = canSubmerge;
    }
    public MyRoom(){}

    public void Fly()
    {
      if(isFlightWorthy)
        Console.WriteLine("Taking off!");
      else
        Console.WriteLine("Falling off cliff!");
    }

    public void GoUnderWater()
    {
      if(isSeaWorthy)
        Console.WriteLine("Diving....");
      else
        Console.WriteLine("Drowning!!!");      
    }
    }
    


           
       
Related examples in the same category
1.Serialize Class to Soap message
2.Use SoapFormatter
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.