Click here to Skip to main content
15,891,372 members
Articles / Web Development / HTML

XML Data Files, XML Serialization, and .NET

Rate me:
Please Sign up or sign in to vote.
4.82/5 (28 votes)
27 Aug 200317 min read 340.2K   6.4K   130  
Describes a means to build XML data files using XML Schema and xsd.exe to facilitate easy XML Serialization
//------------------------------------------------------------------------------
// <autogenerated>
//     This code was generated by a tool.
//     Runtime Version: 1.0.3705.288
//
//     Changes to this file may cause incorrect behavior and will be lost if 
//     the code is regenerated.
// </autogenerated>
//------------------------------------------------------------------------------

// 
// This source code was auto-generated by xsd, Version=1.0.3705.288.
// 
using System;
using System.Xml.Serialization;

namespace CardfileSerializationDemo
{
	/// <remarks/>
	[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/Cardfile.xsd")]
	[System.Xml.Serialization.XmlRootAttribute(Namespace="http://tempuri.org/Cardfile.xsd", IsNullable=false)]
	public class Cards 
	{

		/// <remarks/>
		public PropType Props = new PropType(); // Added new object
    
		/// <remarks/>
		// Added logic to return collection as array
		[System.Xml.Serialization.XmlElementAttribute("Card")]
		public CardType[] Card
		{
			get { return Items.ToArray(); }
			set {
				Items.Clear();
				Items.AddRange(value);
			}
		}

		/// <remarks/>
		public int NextID = 0;
    
		/// <remarks/>
		public string Version = "1.0"; // Added version number of "1.0"

		// Added dirty bit
		[System.Xml.Serialization.XmlIgnore()]
		public bool Dirty = false;

		// Added items collection
		[System.Xml.Serialization.XmlIgnore()]
		public CardCollection Items = new CardCollection();

		// Added default constructor
		public Cards()
		{
		}
	}

	/// <remarks/>
	[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/Cardfile.xsd")]
	public class PropType 
	{
    
		/// <remarks/>
		public string Name = "";
    
		/// <remarks/>
		public string Author = "";
    
		/// <remarks/>
		public string Comments = "";
	}

	/// <remarks/>
	[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/Cardfile.xsd")]
	public class CardTypeBodyContact 
	{
    
		/// <remarks/>
		public string FName = "";
    
		/// <remarks/>
		public string MName = "";
    
		/// <remarks/>
		public string LName = "";
    
		/// <remarks/>
		public string Addr1 = "";
    
		/// <remarks/>
		public string Addr2 = "";
    
		/// <remarks/>
		public string Addr3 = "";
    
		/// <remarks/>
		public string City = "";
    
		/// <remarks/>
		public string State = "";
    
		/// <remarks/>
		public string PCode = "";
    
		/// <remarks/>
		public string Country = "";
     
		/// <remarks/>
		public string Company = "";
   
		/// <remarks/>
		public string HomePh = "";
    
		/// <remarks/>
		public string MobilePh = "";
    
		/// <remarks/>
		public string WorkPh = "";
    
		/// <remarks/>
		public string FaxPh = "";
    
		/// <remarks/>
		public string EMail = "";
    
		/// <remarks/>
		public string Notes = "";
	}

	/// <remarks/>
	[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/Cardfile.xsd")]
	public class CardTypeBody 
	{
    
		/// <remarks/>
		[System.Xml.Serialization.XmlElementAttribute("Image", typeof(System.Byte[]), DataType="base64Binary")]
		[System.Xml.Serialization.XmlElementAttribute("Note", typeof(string))]
		[System.Xml.Serialization.XmlElementAttribute("Contact", typeof(CardTypeBodyContact))]
		public object Item;
	}

	/// <remarks/>
	[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/Cardfile.xsd")]
	public class CardTypeHeader 
	{
    
		/// <remarks/>
		public string Name = "";
    
		/// <remarks/>
		public int ID = -1;
    
		/// <remarks/>
		public CardTypeHeaderType Type;
    
		/// <remarks/>
		public System.DateTime Created;
    
		/// <remarks/>
		public System.DateTime Updated;
	}

	/// <remarks/>
	[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/Cardfile.xsd")]
	public enum CardTypeHeaderType 
	{
    
		/// <remarks/>
		Note,
    
		/// <remarks/>
		Contact,
    
		/// <remarks/>
		Image,
	}

	/// <remarks/>
	[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/Cardfile.xsd")]
	public class CardType 
	{
		// Added default constructor
		public CardType()
		{
		}

		// Added copy constructor
		public CardType(CardType rhs)
		{
			if ( rhs != null )
			{
				// Copy over the header information
				this.Header.Name = String.Format("{0}",rhs.Header.Name); // allocates a new string instead of reference
				this.Header.ID = -1;
				this.Header.Type = rhs.Header.Type;
				this.Header.Created = DateTime.Now;
				this.Header.Updated = DateTime.Now;

				// Copy over the body information
				switch ( rhs.Header.Type )
				{
					case CardTypeHeaderType.Note:
						this.Body.Item = String.Format("{0}",(string)rhs.Body.Item);
						break;

					case CardTypeHeaderType.Image:
						// Allocate a new array
						byte[] bytes = new byte[((byte[])rhs.Body.Item).GetLength(0)];

						// Copy the image bytes over
						((byte[])rhs.Body.Item).CopyTo(bytes,0);

						// Assign the new array to the card
						this.Body.Item = bytes;
						break;

					case CardTypeHeaderType.Contact:
					{
						// Create a new contact object
						CardTypeBodyContact contact = (CardTypeBodyContact)rhs.Body.Item; // casts just once...
						CardTypeBodyContact copy = new CardTypeBodyContact();

						// Copy all fields over
						copy.FName = String.Format("{0}",contact.FName);
						copy.MName = String.Format("{0}",contact.MName);
						copy.LName = String.Format("{0}",contact.LName);
						copy.Addr1 = String.Format("{0}",contact.Addr1);
						copy.Addr2 = String.Format("{0}",contact.Addr2);
						copy.Addr3 = String.Format("{0}",contact.Addr3);
						copy.City = String.Format("{0}",contact.City); 
						copy.State = String.Format("{0}",contact.State);
						copy.PCode = String.Format("{0}",contact.PCode);
						copy.Country = String.Format("{0}",contact.Country);
						copy.Company = String.Format("{0}",contact.Company);
						copy.HomePh = String.Format("{0}",contact.HomePh); 
						copy.MobilePh = String.Format("{0}",contact.MobilePh);
						copy.WorkPh = String.Format("{0}",contact.WorkPh);
						copy.FaxPh = String.Format("{0}",contact.FaxPh); 
						copy.EMail = String.Format("{0}",contact.EMail); 
						copy.Notes = String.Format("{0}",contact.Notes); 

						// Assign the new contact to the card
						this.Body.Item = copy;
					} // scope
						break;
				} // switch
			} // if
		}
    
		/// <remarks/>
		public CardTypeHeader Header = new CardTypeHeader(); // Added new object
    
		/// <remarks/>
		public CardTypeBody Body = new CardTypeBody(); // Added new object
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions