Click here to Skip to main content
15,891,375 members
Articles / Database Development / SQL Server

Using XML Serialization with SQL's FOR XML PATH

Rate me:
Please Sign up or sign in to vote.
4.65/5 (10 votes)
20 Nov 2007CPOL7 min read 79.9K   600   43  
Describes a method for loading data objects using SQL Server 2005's new FOR XML PATH mode and XML serialization.
using System;
using System.Xml.Serialization;
using System.Collections.Generic;

namespace SqlXmlTest {
	/// <summary>Represents an xml root Companies document element.</summary>
	[XmlRoot("Companies")]
	public class CompanyList {
		private List<Company> element_company;

		/// <summary>Company[] Company xml element.</summary>
		[XmlElement("Company")]
		public List<Company> Companies {
			get { return this.element_company; }
			set { this.element_company = value; }
		}
	}

	/// <summary>Represents a Companies.Company node.</summary>
	public class Company {
		private System.Int32 attribute_id;
		private System.String attribute_name;
		private Address element_address;
		private List<Contact> element_contacts;

		/// <summary>System.Int32 id attribute.</summary>
		[XmlAttribute("id")]
		public System.Int32 Id {
			get { return this.attribute_id; }
			set { this.attribute_id = value; }
		}

		/// <summary>System.String name attribute.</summary>
		[XmlAttribute("name")]
		public System.String Name {
			get { return this.attribute_name; }
			set { this.attribute_name = value; }
		}

		/// <summary>Address xml element.</summary>
		[XmlElement("Address")]
		public Address Address {
			get { return this.element_address; }
			set { this.element_address = value; }
		}

		/// <summary>Contact[] Contacts xml array element.</summary>
		[XmlArrayItem("Contact", typeof(Contact))]
		[XmlArray("Contacts")]
		public List<Contact> Contacts {
			get { return this.element_contacts; }
			set { this.element_contacts = value; }
		}
	}

	/// <summary>Represents a Company.Address node.</summary>
	public class Address {
		private System.String element_street;
		private System.String element_city;
		private System.String element_state;
		private System.String element_zip;

		/// <summary>String Street element.</summary>
		[XmlElement("Street")]
		public System.String Street {
			get { return this.element_street; }
			set { this.element_street = value; }
		}

		/// <summary>String City element.</summary>
		[XmlElement("City")]
		public System.String City {
			get { return this.element_city; }
			set { this.element_city = value; }
		}

		/// <summary>String State element.</summary>
		[XmlElement("State")]
		public System.String State {
			get { return this.element_state; }
			set { this.element_state = value; }
		}

		/// <summary>String Zip element.</summary>
		[XmlElement("Zip")]
		public System.String Zip {
			get { return this.element_zip; }
			set { this.element_zip = value; }
		}
	}

	/// <summary>Represents a Contacts.Contact node.</summary>
	public class Contact {
		private System.Int32 attribute_id;
		private System.String attribute_name;
		private ContactInfo element_contactinfo;
		private Login element_login;

		/// <summary>System.Int32 id attribute.</summary>
		[XmlAttribute("id")]
		public System.Int32 Id {
			get { return this.attribute_id; }
			set { this.attribute_id = value; }
		}

		/// <summary>System.String name attribute.</summary>
		[XmlAttribute("name")]
		public System.String Name {
			get { return this.attribute_name; }
			set { this.attribute_name = value; }
		}

		/// <summary>ContactInfo xml element.</summary>
		[XmlElement("ContactInfo")]
		public ContactInfo ContactInfo {
			get { return this.element_contactinfo; }
			set { this.element_contactinfo = value; }
		}

		/// <summary>Login xml element.</summary>
		[XmlElement("Login")]
		public Login Login {
			get { return this.element_login; }
			set { this.element_login = value; }
		}
	}

	/// <summary>Represents a Contact.ContactInfo node.</summary>
	public class ContactInfo {
		private System.String attribute_email;
		private System.String attribute_phone;

		/// <summary>System.String email attribute.</summary>
		[XmlAttribute("email")]
		public System.String Email {
			get { return this.attribute_email; }
			set { this.attribute_email = value; }
		}

		/// <summary>System.String phone attribute.</summary>
		[XmlAttribute("phone")]
		public System.String Phone {
			get { return this.attribute_phone; }
			set { this.attribute_phone = value; }
		}
	}

	/// <summary>Represents a Contact.Login node.</summary>
	public class Login {
		private System.String attribute_uid;
		private System.String attribute_pwd;

		/// <summary>System.String uid attribute.</summary>
		[XmlAttribute("uid")]
		public System.String Uid {
			get { return this.attribute_uid; }
			set { this.attribute_uid = value; }
		}

		/// <summary>System.String pwd attribute.</summary>
		[XmlAttribute("pwd")]
		public System.String Pwd {
			get { return this.attribute_pwd; }
			set { this.attribute_pwd = value; }
		}
	}
}

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) BoneSoft Software
United States United States
I've been in software development for more than a decade now. Originally with ASP 2.0 and VB6. I worked in Japan for a year doing Java. And have been with C# ever since.

In 2005 I founded BoneSoft Software where I sell a small number of developer tools.
This is a Organisation (No members)


Comments and Discussions