WebMasterSam

.Net, SEO, Dynamics CRM, AdSense/AdWords, Dating sites, Silverlight, Web hosting and more

About me

I'm an IT consultant working primarily with the .Net Framework as a developper and architect. I also work on my own on my personnal dating websites. I've been developping websites since 2000.

If you like what I do, feel free to support me

PayPal - The safer, easier way to pay online!

Bookmark

Bookmark and Share

Sponsored links

Amazon hot deals

Computer releases

Last comments

Comment RSS

How .Net XML serialization works ?



.Net serialiazation is very powerful and simple to use. It can be tedious to serialize complex things, but for simple things, it helps a lot.

First, XML and binary serialization is 2 different serialization process. I will discuss binary serialization in another article, but first, let's explain XML serialization.

So, what's XML serialization ?
Serialization is the way to save an object to an XML form, like an XML document. When serializing, the .Net Framework takes all the public read/write properties and create an XML document that it saves to disc. This is the normal process, but there is more things you can do when serializing, like saving to a MermoyStream to get the XML string instead of saving it to disc. This process is very fast but not as fast as the binary serialization. Another thing, XML serialization is fun to use because when you serialize something, you can read everything, you visualy see the object you've just serialized.

Let's see an example in code
First, we have to declare a class from which we will instanciate an object and do the serialization with it. For the example, I declared a class named "SerializableClass".

[Serializable()]
public class SerializableClass
{
    private string _property1 = string.Empty;
    private int _property2 = 4;

    public SerializableClass()
    {
        // Default constructor : Must be present
    }

    public string Property1
    {
        get { return _property1; }
        set { _property1 = value; }
    }

    public int Property2
    {
        get { return _property2; }
        set { _property2 = value; }
    }
}


Note that I explicitly declare the default constructor to emphasize the fact that every class that we want to serialize must have a default constructor. The reason for this is because when the deserialization process takes place, the .Net Framework first instanciates an empty object of the type we want to deserialize and affects all properties on by one.

You probably saw that I marked my class with the class attribute "Serializable". This is necessary and it tells that the object can be serialized. To be serializable, a class and all of its members must be serializable (or primitive - string, int, etc...). For example, if I have a class that has a member of type System.Collections.Generic.List<T>, even if I mark my class "Serializable", my class will not be serializable because "List<T>" is not marked as serializable.

Now let's do the few lines to serialize a "SerializableClass" object.

SerializableClass obj = new SerializableClass();
XmlSerializer serializer = new XmlSerializer(obj.GetType());
XmlTextWriter writer = new XmlTextWriter("C:/obj.xml", Encoding.Default);

serializer.Serialize(writer, obj);

writer.Close();


It's done ! Simple huh ? To seralize an object to an XML document is very easy and fast. If we open the XML document created by those lines, it would look like this :

<?xml version="1.0" encoding="Windows-1252"?>
<SerializableClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Property1 />
    <Property2>4</Property2>
</SerializableClass>


Everything needed to do this example can be found in those namespaces.

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


The process of serialization is normaly as simple as that, but when you have complex structures with objects as members, you sometimes get strange things. Sometimes serialization works (and it should not !) and deserialization fails. XML serialization is always more tedious to get working than the binary serialization because every property must be serialized as text so we can read it.

Posted: Mar 09 2009, 12:42 by WebMasterSam | Comments (45) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: .Net
Social Bookmarks: E-mail | Kick it! | DZone it! | del.icio.us

Do I have to buy things on "AdSense secrets" sites to make money ?



You probably saw a lot of times websites with titles like "Google AdSense secrets revealed" or "Making millions with Google AdSense". Those sites encourage you to buy their books, their video advices and other stuff. All they want to do is make money with you, while you think you're learning something to make more money than them.

First of all, stop thinking AdSense is the way to make millions for everyone. Yes, some people makes millions with this program, like Markus Frind (PlentyOfFish), but don't think because you read books and books you will make millions too ! By doing this you will only help people getting richer and richer selling you something you can get for free.

All you need to know to make some money with Google AdSense is everywhere on the net, and free ! There is only some simple rules to follow to succesfully make some money with this. There's not an absolute formula that works every time. Everything is different from sites to sites, you have to try it yourself.

By experience, if you want to make money with Google AdSense you have to try many different formulas. You have to change the size of your ads, the places where you put them, the colors, etc... Each website is different, so every website needs a different advertising strategie. You can find some good ideas if you Google the web for "AdSense tips", but the best school you can have is to visit websites that uses the Google AdSense program and try to see how they places their ads.

The most important thing you need to remember from this article is that you never have to pay to get good AdSense tips. You can find everything you need for free.

Posted: Mar 06 2009, 14:19 by WebMasterSam | Comments (108) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Social Bookmarks: E-mail | Kick it! | DZone it! | del.icio.us