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.

through the SoapFormatter class) saves data to a text stream, automatically encod<strong>in</strong>g<strong>in</strong><strong>for</strong>mation <strong>in</strong> a SOAP message be<strong>for</strong>e writ<strong>in</strong>g.The SOAP FormatterTo use the SOAP <strong>for</strong>matter, you must reference a dist<strong>in</strong>ct assembly—System.Runtime.Serialization.Formatters.Soap. You add this separate assemblythrough the Add Reference dialog box or manually on the compiler's command l<strong>in</strong>ethrough the /reference switch. In addition to l<strong>in</strong>k<strong>in</strong>g the assembly to the project, you stillhave to import the namespace with the same name as the assembly, as shown here:us<strong>in</strong>g System.Runtime.Serialization.Formatters.Soap;At this po<strong>in</strong>t, you prepare the output stream, <strong>in</strong>stantiate the SOAP <strong>for</strong>matter, and callthe Serialize method, as follows:// emp is the object <strong>in</strong>stance to processStreamWriter writer = new StreamWriter(filename);SoapFormatter soap = new SoapFormatter();soap.Serialize(writer.BaseStream, emp);writer.Close();Note that the Serialize method accepts only a stream object, which makes serializ<strong>in</strong>g to<strong>in</strong>-memory str<strong>in</strong>gs a little more difficult.Let's consider a rather simple class, such as the follow<strong>in</strong>g Employee class:[Serializable]public class Employee{public <strong>in</strong>t ID;public str<strong>in</strong>g FirstName;public str<strong>in</strong>g LastName;public str<strong>in</strong>g Position;public <strong>in</strong>t[] Territories;}Upon <strong>in</strong>stantiation, only the numeric ID field has a determ<strong>in</strong>ed value (0). All the othermembers are null, as shown here:Employee emp = new Employee();After the Employee class has been <strong>in</strong>stantiated, the SOAP <strong>for</strong>matter generates thefollow<strong>in</strong>g script:389

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

Saved successfully!

Ooh no, something went wrong!