C# Tutorials and offshore development in India
Tutorials Resources Forum Reviews Communities Interview Jobs Projects Training Your Ad Here


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Serialization »

NonSerializedAttribute in Serialization


Posted Date: 09 Nov 2009    Resource Type: Code Snippets    Category: Serialization
Author: tamil selviMember Level: Gold    
Rating: 1 out of 5Points: 5



This example helps to prevent the filed from serialization.



To prevent the field from being serialized,when using BinaryFormatter or SoapFormatter classes to serialize an object, we can use NonSerializedAttribute




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


here you can find the code No Serialization






public class dotseri {
public static void Main() {

Dotnetspider seriobj = new Dotnetspider ();

Console.WriteLine("Before serialization: ");
seriobj.Show();
Stream stream = File.Open("sample.xml", FileMode.Create);
SoapFormatter formatter = new SoapFormatter();

formatter.Serialize(stream, seriobj);
stream.Close();

seriobj = null;

stream = File.Open("data.xml", FileMode.Open);
formatter = new SoapFormatter();

seriobj = (Dotnetspider)formatter.Deserialize(stream);
stream.Close();

Console.WriteLine("");
Console.WriteLine("After deserialization: ");
seriobj.Show();
}
}

[Serializable()]
public class Dotnetspider {

public int Sno;
public string Name;
public string Address1;


[NonSerialized()] public string Address2;


// Address2 is not serialized
public Dotnetspider() {

Sno = 11;
Name = "sri";
Address1 = "5th st";
Address2 = "chennai";

}

public void Show() {

Console.WriteLine("Sno = '{0}'", Sno);
Console.WriteLine("Name = '{0}'", Name);
Console.WriteLine("Address1 = '{0}'", Address1);
Console.WriteLine("Address2 = '{0}'", Address2);
}
}




Responses to the resource: "NonSerializedAttribute in Serialization"

No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
NonSerializedAttribute  .  SoapFormatter  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Soap Formatter
Previous Resource: Serialization & DeSerialization
Return to Discussion Resource Index
Post New Resource
Category: Serialization


Post resources and earn money!
 
More Resources




About Us    Contact Us    Privacy Policy    Terms Of Use