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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The XmlTextWriter object is still a stream-based component that needs a dest<strong>in</strong>ation towrite to. Unlike the Bitmap object, however, the XmlTextWriter object can be <strong>for</strong>ced towrite the output to a str<strong>in</strong>g. To do that, you <strong>in</strong>itialize the <strong>XML</strong> text writer with an <strong>in</strong>stanceof the Str<strong>in</strong>gWriter object. The f<strong>in</strong>al str<strong>in</strong>g with the <strong>XML</strong> code can be obta<strong>in</strong>ed with a callto the Str<strong>in</strong>gWriter object's ToStr<strong>in</strong>g method.The <strong>for</strong>mat of the <strong>XML</strong> text returned is shown here:FFD8FF...E00010Notice that the comment and the size of the file are strictly call-specific parameters. TheSize attribute refers to the size of the B<strong>in</strong>Hex-encoded text. As you'd expect, this valueis significantly larger than JPEG size. Hav<strong>in</strong>g that value available is not strictlynecessary, but once it's on the client, it can simplify the task of trans<strong>for</strong>m<strong>in</strong>g the <strong>XML</strong>stream back <strong>in</strong>to a JPEG image.Str<strong>in</strong>gWriter and Unicode Encod<strong>in</strong>gThe <strong>XML</strong> output generated by the GetSalesReportBarChart method uses the Unicodeencod<strong>in</strong>g scheme—UTF-16—<strong>in</strong>stead of the default UTF-8. This would be f<strong>in</strong>e if not <strong>for</strong>the fact that <strong>Microsoft</strong> Internet Explorer returns an error when you double-click the<strong>XML</strong> file. The error has noth<strong>in</strong>g to do with the <strong>XML</strong> itself; it is more a bug (or perhapseven a feature) of Internet Explorer and the <strong>in</strong>ternal style sheet Internet Explorer usesto display <strong>XML</strong> documents.In general, UTF-16 is used whenever you write <strong>XML</strong> text to a Str<strong>in</strong>gWriter object.When a TextWriter object (Str<strong>in</strong>gWriter <strong>in</strong>herits from TextWriter) is passed to theXmlTextWriter constructor, no explicit encod<strong>in</strong>g argument is allowed. In this case, theXmlTextWriter object transparently <strong>in</strong>herits the encod<strong>in</strong>g set conta<strong>in</strong>ed <strong>in</strong> the writerobject be<strong>in</strong>g passed. The Str<strong>in</strong>gWriter class hard-codes its Encod<strong>in</strong>g property to UTF-16—there's no way <strong>for</strong> you to change it, because the property is marked as read-only.If you want to generate <strong>XML</strong> str<strong>in</strong>gs with an encod<strong>in</strong>g scheme other than UTF-16,drop Str<strong>in</strong>gWriter objects <strong>in</strong> favor of memory streams.The helper class shared by the remotable object and the Web service is now ready touse. Let's look more closely at the remote service component.Writ<strong>in</strong>g the Remote Service ComponentAs mentioned, a remotable component has just one requirement: the class thatrepresents the object must be <strong>in</strong>herited from MarshalByRefObject. Unless you need toexercise stricter control over the object lifetime, you don't need to override any of themethods def<strong>in</strong>ed <strong>in</strong> the base class <strong>for</strong> MBR objects.Apart from the parent class, a remotable class is not different from any other class <strong>in</strong>the .<strong>NET</strong> Framework. All of its public methods are callable by clients, the class canimplement any number and any type of <strong>in</strong>terfaces, and the class can reference anyother external class.Because we already put all the core code <strong>in</strong> the SalesDataProvider class, writ<strong>in</strong>g theremote service class—ServiceSalesProvider—is a snap. The class is a simple wrapper<strong>for</strong> SalesDataProvider, as shown here:public class ServiceSalesProvider : MarshalByRefObject437

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

Saved successfully!

Ooh no, something went wrong!