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.

XmlTextWriter provides a standard implementation <strong>for</strong> all the methods and theproperties described up to now, plus a few more. It ma<strong>in</strong>ta<strong>in</strong>s an <strong>in</strong>ternal stack to keeptrack of <strong>XML</strong> elements that have been opened but not yet closed. Each element nodecan be directly associated with a namespace, thus becom<strong>in</strong>g the root of a namespacescope. If a namespace is not specified, the element is associated with the last declarednamespace.The XmlTextWriter class has three constructors. You can have the writer work on a fileor on an open stream. In both cases, you must also specify the required encod<strong>in</strong>gschema, as shown <strong>in</strong> the follow<strong>in</strong>g code. If this argument is null, the UniversalCharacter Set Trans<strong>for</strong>mation Format, 8-bit <strong>for</strong>m (UTF-8) character encod<strong>in</strong>g set isassumed.public XmlTextWriter(Stream w, Encod<strong>in</strong>g encod<strong>in</strong>g);public XmlTextWriter(str<strong>in</strong>g filename, Encod<strong>in</strong>g encod<strong>in</strong>g);The third constructor allows you build an <strong>XML</strong> text writer start<strong>in</strong>g from a TextWriterobject.Encod<strong>in</strong>g SchemasIn the .<strong>NET</strong> Framework, four different character encod<strong>in</strong>g schemas are def<strong>in</strong>ed. Eachschema corresponds to a class that <strong>in</strong>herits from the Encod<strong>in</strong>g class. The classes arelisted <strong>in</strong> Table 4-6.Table 4-6: Available Character Encod<strong>in</strong>g SchemasProperty Class DescriptionEncod<strong>in</strong>g.ASCII ASCIIEncod<strong>in</strong>g Encodes Unicode charactersas s<strong>in</strong>gle 7-bit ASCIIcharacters.Encod<strong>in</strong>g.Unicode UnicodeEncod<strong>in</strong>g Encodes each Unicodecharacter as two consecutivebytes.Encod<strong>in</strong>g.UTF7 UTF7Encod<strong>in</strong>g Encodes Unicode charactersus<strong>in</strong>g the UTF-7 characterencod<strong>in</strong>g set. (UTF-7 stands<strong>for</strong> Universal Character SetTrans<strong>for</strong>mation Format, 7-bit<strong>for</strong>m.)Encod<strong>in</strong>g.UTF8 UTF8Encod<strong>in</strong>g Encodes Unicode charactersus<strong>in</strong>g the UTF-8 characterencod<strong>in</strong>g set.The default character encod<strong>in</strong>g schema is UTF-8, which supports all Unicode charactervalues and surrogates. UTF-8 uses a variable number of bytes per character and isoptimized <strong>for</strong> the lower 127 ASCII characters.If you want to use the default encod<strong>in</strong>g, omit the second argument <strong>in</strong> the constructor.Otherwise, use the static properties of the Encod<strong>in</strong>g class to <strong>in</strong>dicate which type ofencod<strong>in</strong>g you want. You don't need to create a new <strong>in</strong>stance of an encod<strong>in</strong>g class tocreate a writer that encodes data <strong>in</strong> a certa<strong>in</strong> way. For example, to create an ASCIIstream, you use the follow<strong>in</strong>g code:XmlTextWriter xmlw = new XmlTextWriter(file, Encod<strong>in</strong>g.ASCII);119

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

Saved successfully!

Ooh no, something went wrong!