Page 1 of 1

Serializing an object with XML and SOAP

#1 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Post icon  Posted 07 April 2009 - 03:36 PM

Sometimes you might want to write out an object using XML with out coding it by hand. Serializing is an easy way to do that. By serializing an object you create a static picture of that object at that moment. There are two ways to serialize an object. One way is binary serialization, the other is using XML and SOAP. Here I will discuss the later.

Using this form of serialization you can store the state of any public fields and properties of a public class. It will not store any other information. It will create a simple XML document.

Let's say that you have a class called Person. This the code for that class:

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

namespace XMLandSOAP
{
	public class Person
	{
		// These will be serialized
		public string Name;
		public int Age;
		public string Email;
		public string City;

		// These will not be serialized
		private string socialSecurityNumber;
		private string bankAccountNumber;
		private string bankName;

		// This will be serialized		
		public string BankName
		{
			get { return bankName; }
			set { bankName = value; }
		}

		public Person()
		{
			Name = "Jane Doe";
			Age = 18;
			Email = "janedoe@ficticious.ca";
			City = "Perth";
			socialSecurityNumber = "1935469";
			bankAccountNumber = "193-98457";
			bankName = "Bank of Canada";
		}

	}
}



I don't know what an American Social Security number looks like. :rolleyes:
To serialize this class you will need to add two using statements.

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



The first one adds the ability to serialize the object. The second is needed because you will need a stream to serialize the object.

The code to serialize the object is simple you can add it to the class. This is the code:

public void WritePerson()
{
	XmlSerializer person = new XmlSerializer();

	StreamWriter stream = new StreamWriter("Person.XML");
	person.Serialize(stream, this);
	stream.Close();
}



What the code does is create a XMLSerializer, then a StreamWriter, then it serializes the object and finally it closes the stream.

To retrieve the serialized document you use a process called deserialization, it is the reverse of serialization. Again, it is fairly simple. You can add this method to your class:

public Person ReadPerson()
{
	XmlSerializer person = new XmlSerializer();
	Person tempPerson = new Person();

	StreamReader stream = new StreamReader("Person.XML");
	tempPerson = (Person)person.Deserialize(stream);
	stream.Close();
	return tempPerson;
}



First you create an XmlSerializer. Then you need to create a temporary object to hold the object that is being deserialized. Then you create a StreamReader. The Deserialize method returns an object that you must cast. You close the stream and return the object.

This is a very simple way to save a public class and it's public fields and properties to a simple XML file.

Is This A Good Question/Topic? 0
  • +

Replies To: Serializing an object with XML and SOAP

#2 Ticon  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 320
  • Joined: 20-August 09

Posted 30 December 2010 - 03:15 PM

I love the simplicity of this tutorial. But i get errors when including using System.Xml.<span class="searchlite">Serialization</span>;
I use visual studio 2010 but im struck dumb on this.
Was This Post Helpful? 0
  • +
  • -

#3 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Posted 30 December 2010 - 03:45 PM

This tutorial was written using Visual C# 2005. It hasn't been updated to use either Visual C# 2008 or 2010. I do know there was a change in the classes between 2005 and 2008. I will see about updating the code to use C# 4.0.
Was This Post Helpful? 0
  • +
  • -

#4 Ticon  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 320
  • Joined: 20-August 09

Posted 30 December 2010 - 04:03 PM

Thank you, Much appreciated friend.
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6049
  • View blog
  • Posts: 23,475
  • Joined: 23-August 08

Posted 30 December 2010 - 04:08 PM

Please tell me you didn't copy and paste
using System.Xml.<span class="searchlite">Serialization</span>;

exactly as it appears into your code.
Was This Post Helpful? 0
  • +
  • -

#6 Ticon  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 320
  • Joined: 20-August 09

Posted 30 December 2010 - 04:24 PM

View PostJackOfAllTrades, on 30 December 2010 - 03:08 PM, said:

Please tell me you didn't copy and paste
using System.Xml.<span class="searchlite">Serialization</span>;

exactly as it appears into your code.


Why yes I did, I wouldn't think that pasting a using could go wrong. however I did do my little part of playing with it trying to correct it but to no avail. I am noob anyways..
Was This Post Helpful? 0
  • +
  • -

#7 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Posted 30 December 2010 - 04:33 PM

That would be the problem there. Think I need to put my glasses on, didn't notice that. The code for Serializing and Deserializing has changed with C# 4.0. That I do have new code for. This is what the code for writing out and reading in an object. I would also point out it would make sense to use generics here but I'm not going to go into that. :)

You will need to add this using statement:

using System.Xml;



Writing out a Person object with XML:

        public void WritePerson(Person person, string filename)
        {
            XmlWriter writer = XmlWriter.Create(filename);
            XmlSerializer serializer = new XmlSerializer(typeof(Person));
            serializer.Serialize(writer, person);
            writer.Close();
        }



Reading in a Person object with XML:

        public static Person ReadPerson(string filename)
        {
            StreamReader reader = new StreamReader(filename);
            XmlSerializer serlializer = new XmlSerializer(typeof(Person));

            Person person = (Person)serlializer.Deserialize(reader);
            reader.Close();
            return person;
        }


Was This Post Helpful? 0
  • +
  • -

#8 Ticon  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 320
  • Joined: 20-August 09

Posted 02 February 2011 - 02:40 PM

Edit: I fixed it Sorry to edit 3 times great tutorial my friend!

This post has been edited by Ticon: 02 February 2011 - 03:30 PM

Was This Post Helpful? 0
  • +
  • -

#9 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 299
  • View blog
  • Posts: 1,768
  • Joined: 26-March 09

Posted 30 June 2011 - 08:59 AM

Firstly, thanks for the tutorial as it's given me an insight into what I think I need to do to achieve my objective.

I'm a complete newbie when it comes to Webservices and the like, but yet find myself needing to write one.

The service needs to take a "document" (header + multiple lines) input, modify it and return the modified version.

Using your tutorial I was thinking of creating a class to represent the "document" using public variables for the header and either a DataTable or Array of a class for the lines.

I'm happy doing that, but I'm struggling understanding how that would be implemented in the web service.

Using your test class, I've added a web method of type Person that will take a parameter of type Person.

    [WebMethod]
    public Person GetPerson(Person p) {
       // Change age
        p.Age = 34;


        return p;
    }


Would that be the correct way to do it or am I misunderstanding it completely as I don't see where the ReadPerson and WritePerson would fit in.

Any pointers would be useful.

Thanks.

This post has been edited by maj3091: 30 June 2011 - 09:20 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1