13.07.2015 Views

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

command, the data flows <strong>in</strong>to the serializer, and an <strong>in</strong>stance of a particular class isreturned.Employee emp = LoadEmployeeData(empID);The follow<strong>in</strong>g code shows the body of the LoadEmployeeData method:private Employee LoadEmployeeData(<strong>in</strong>t empID){// Create the serializerXmlSerializer ser = PrepareEmployeeTypeSerializer();// Prepare the connection and the SQL commandSqlConnection conn = new SqlConnection(NW<strong>in</strong>dConnection);SqlCommand cmd = PrepareSqlCommand(empID, conn);conn.Open();// Execute the commandEmployee emp = null;XmlTextReader reader =(XmlTextReader) cmd.ExecuteXmlReader();// Deserialize the <strong>in</strong>com<strong>in</strong>g dataif(ser.CanDeserialize(reader))emp = (Employee) ser.Deserialize(reader);elseConsole.WriteL<strong>in</strong>e("Cannot deserialize");// Clean-upreader.Close();conn.Close();}return emp;The serializer is tailor-made <strong>for</strong> the Employee class shown here:public class Employee{public str<strong>in</strong>g FirstName;public str<strong>in</strong>g LastName;public str<strong>in</strong>g Position;public DateTime Hired;}The SQL command used <strong>in</strong> our example is shown here:SELECT firstname, lastname, title, hiredate FROM employees412

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!