Click here to Skip to main content
15,889,808 members
Home / Discussions / C#
   

C#

 
AnswerRe: .NET Code Easy to Disassemble Pin
Dave Kreskowiak29-Sep-05 11:30
mveDave Kreskowiak29-Sep-05 11:30 
GeneralRe: .NET Code Easy to Disassemble Pin
Heinz_29-Sep-05 19:36
Heinz_29-Sep-05 19:36 
AnswerRe: .NET Code Easy to Disassemble Pin
Ashok Dhamija29-Sep-05 15:19
Ashok Dhamija29-Sep-05 15:19 
GeneralRe: .NET Code Easy to Disassemble Pin
Heinz_29-Sep-05 19:43
Heinz_29-Sep-05 19:43 
QuestionFastest way to read XML Pin
tommazzo29-Sep-05 8:39
tommazzo29-Sep-05 8:39 
AnswerRe: Fastest way to read XML Pin
turbochimp29-Sep-05 9:02
turbochimp29-Sep-05 9:02 
GeneralRe: Fastest way to read XML Pin
tommazzo29-Sep-05 10:51
tommazzo29-Sep-05 10:51 
GeneralRe: Fastest way to read XML Pin
turbochimp29-Sep-05 11:42
turbochimp29-Sep-05 11:42 
Why do you say that using XML serialization would be out of the question just because you have some properties you might not want to serialize?

If you have properties you want to hide during serialization, then just mark them with the XmlIgnoreAttribute.

There are obvious advantages to using XML serialization besides the fact that it's probably faster than rolling your own solution. It's vastly more tolerant of changes to structure of the class being serialized, and almost impossible to accidentally create malformed XML.

Here's an example - enjoy...:

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

namespace XmlSerializationExample
{
	class Example
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			MyClass mc = new MyClass();
			mc.Property1 = "a";
			mc.Property2 = "b";
			mc.Property3 = "c";
			mc.PickANumber = 1234;
			mc.IgnoreThisProperty = "Blahblahblahblah.";
			mc.Save(@"c:\serialized_myclass.xml");

			MyClass mc2 = MyClass.Load(@"c:\serialized_myclass.xml");
			Console.WriteLine("Deserialized Property1: {0}", mc2.Property1);
			Console.WriteLine("Deserialized Property2: {0}", mc2.Property2);
			Console.WriteLine("Deserialized Property3: {0}", mc2.Property3);
			Console.WriteLine("Deserialized PickANumber: {0}", mc2.PickANumber);
			Console.WriteLine("Deserialized ComboProperty: {0}", mc2.ComboProperty);
			Console.WriteLine("Deserialized IgnoreThisProperty: {0}", mc2.IgnoreThisProperty);

			Console.ReadLine();
		}
	}

	public class MyClass
	{
		#region Fields
		
		// These fields will not be directly serialized - they are private
		private string _field1;
		private string _field2;
		private string _field3;
		private string _ignoredField;
		
		// This field will be directly serialized, since it is public
                // (don't do this - bad practice)
		public int PickANumber; 

		#endregion Fields
		
		#region Constructor
		
		public MyClass(){}
		
		#endregion Constructor

		#region Properties
		
		/// <summary>
		/// Property1 will be serialized because it is a public property with 'get' and 'set' accessors.
		/// </summary>
		public string Property1
		{
			get{return _field1;}
			set{_field1 = value;}
		}

		/// <summary>
		/// Property2 will be serialized because it is a public property with 'get' and 'set' accessors.
		/// </summary>
		public string Property2
		{
			get{return _field2;}
			set{_field2 = value;}
		}

		/// <summary>
		/// Property3 will be serialized because it is a public property with 'get' and 'set' accessors.
		/// </summary>
		public string Property3
		{
			get{return _field3;}
			set{_field3 = value;}
		}

		/// <summary>
		/// Naughty, naughty property - I shall ignore you!
		/// </summary>
		[XmlIgnore]
		public string IgnoreThisProperty
		{
			get{return _ignoredField;}
			set{_ignoredField = value;}
		}

		/// <summary>
		/// ComboProperty will NOT be serialized because it is a public property with only a 'get' accessor.
		/// </summary>
		public string ComboProperty
		{
			get{return string.Format("{0}{1}{2}", Property1, Property2, Property3);}
		}

		#endregion Properties
		
		#region Methods
		
		public void Save(string path)
		{
			// Saves the class instance to disk.
			XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
			using(StreamWriter writer = new StreamWriter(path, false))
			{
				serializer.Serialize(writer, this); // assumes myInstance is an object of the type MyClass...
				writer.Close();
			}
		}

		public static MyClass Load(string path)
		{
			// Loads the class instance XML file from disk and deserializes it
			MyClass instance = null;
			if(File.Exists(path))
			{
				XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
				using(StreamReader reader = new StreamReader(path))
				{
					instance = serializer.Deserialize(reader) as MyClass;
					reader.Close();
				}
			}
			return instance;
		}
		
		#endregion Methods
	}
}


The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’

GeneralRe: Fastest way to read XML Pin
tommazzo30-Sep-05 4:47
tommazzo30-Sep-05 4:47 
GeneralRe: Fastest way to read XML Pin
turbochimp30-Sep-05 9:31
turbochimp30-Sep-05 9:31 
GeneralRe: Fastest way to read XML Pin
tommazzo30-Sep-05 23:15
tommazzo30-Sep-05 23:15 
QuestionIntegration Updater Appication block with Enterprise Library Pin
davidalcaraz29-Sep-05 8:18
davidalcaraz29-Sep-05 8:18 
QuestionPARTICULAR HELP! CAN ANYONE GUIDE ME? Pin
_Comet_Keeper_29-Sep-05 7:02
_Comet_Keeper_29-Sep-05 7:02 
AnswerRe: PARTICULAR HELP! CAN ANYONE GUIDE ME? Pin
Judah Gabriel Himango29-Sep-05 7:44
sponsorJudah Gabriel Himango29-Sep-05 7:44 
AnswerRe: PARTICULAR HELP! CAN ANYONE GUIDE ME? Pin
WillemM29-Sep-05 7:48
WillemM29-Sep-05 7:48 
AnswerYES... VERY HARD... Pin
_Comet_Keeper_29-Sep-05 9:54
_Comet_Keeper_29-Sep-05 9:54 
GeneralRe: YES... VERY HARD... Pin
Dave Kreskowiak29-Sep-05 10:12
mveDave Kreskowiak29-Sep-05 10:12 
QuestionReading from Excel (quick question) Pin
BECK729-Sep-05 6:20
BECK729-Sep-05 6:20 
AnswerRe: Reading from Excel (quick question) Pin
turbochimp29-Sep-05 8:22
turbochimp29-Sep-05 8:22 
GeneralRe: Reading from Excel (quick question) Pin
BECK729-Sep-05 9:47
BECK729-Sep-05 9:47 
GeneralRe: Reading from Excel (quick question) Pin
turbochimp29-Sep-05 10:58
turbochimp29-Sep-05 10:58 
GeneralRe: Reading from Excel (quick question) Pin
BECK729-Sep-05 11:20
BECK729-Sep-05 11:20 
GeneralRe: Reading from Excel (quick question) Pin
turbochimp29-Sep-05 12:12
turbochimp29-Sep-05 12:12 
GeneralRe: Reading from Excel (quick question) Pin
BECK729-Sep-05 12:16
BECK729-Sep-05 12:16 
QuestionRowFilter property Pin
zaboboa29-Sep-05 5:52
zaboboa29-Sep-05 5:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.