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.

epeatedly <strong>in</strong> the same document. Whenever possible, applicationsshould use the namespace name rather than a prefix. The use of aprefix is more acceptable when only unique prefixes are used andpossibly only one namespace is def<strong>in</strong>ed <strong>in</strong> the document.Writ<strong>in</strong>g Encoded DataAs mentioned <strong>in</strong> the section "Methods of the XmlWriter Class," on page 141, the <strong>XML</strong>text writer object has two methods that write out <strong>XML</strong> data <strong>in</strong> a softly encrypted wayus<strong>in</strong>g base64 and B<strong>in</strong>Hex algorithms. The methods <strong>in</strong>volved—WriteBase64 andWriteB<strong>in</strong>Hex—have a rather straight<strong>for</strong>ward <strong>in</strong>terface. They simply take an array ofbytes and write it out start<strong>in</strong>g at a specified offset and <strong>for</strong> the specified number of bytes.(As you saw <strong>in</strong> Chapter 2, <strong>XML</strong> reader classes have match<strong>in</strong>g ReadBase64 andReadB<strong>in</strong>Hex methods to com<strong>for</strong>tably read back encoded <strong>in</strong><strong>for</strong>mation.)Note In the .<strong>NET</strong> Framework, base64 encod<strong>in</strong>g can also be per<strong>for</strong>medthrough static methods exposed by the Convert class. In particular,the ToBase64Str<strong>in</strong>g method takes an array of bytes and returns abase64-encoded str<strong>in</strong>g. Likewise, the FromBase64Str<strong>in</strong>g methoddecodes a previously encoded str<strong>in</strong>g and returns it as an array ofbytes. For some reason, the .<strong>NET</strong> Framework does not providesimilar support <strong>for</strong> B<strong>in</strong>Hex. B<strong>in</strong>Hex, there<strong>for</strong>e, is supported onlythrough <strong>XML</strong> readers and writers.In the section "The <strong>XML</strong> Writer <strong>Programm<strong>in</strong>g</strong> Interface," on page 136, you learned howto serialize an array of str<strong>in</strong>gs to <strong>XML</strong> us<strong>in</strong>g the follow<strong>in</strong>g array:str<strong>in</strong>g[] theArray = {"Rome", "New York", "Sydney","Stockholm", "Paris"};Let's look at how to write this array to a base64-encoded <strong>for</strong>m. The structure of thecode we analyzed earlier does not need to be altered much. Only a couple of issuesneed to be addressed. The first concerns how str<strong>in</strong>gs are actually turned <strong>in</strong>to an arrayof bytes. The second concerns the signature of the encod<strong>in</strong>g methods. You can useWriteB<strong>in</strong>Hex to write both element and attribute content <strong>in</strong> B<strong>in</strong>Hex <strong>for</strong>mat, <strong>in</strong>stead ofus<strong>in</strong>g WriteBase64, as shown here:XmlTextWriter xmlw = new XmlTextWriter(filename, null);writer.Formatt<strong>in</strong>g = Formatt<strong>in</strong>g.Indented;writer.WriteStartDocument();writer.WriteComment("Array to Base64 <strong>XML</strong>");writer.WriteStartElement("array");writer.WriteAttributeStr<strong>in</strong>g("xmlns", "x", null,"d<strong>in</strong>oe:isbn-0735618011");<strong>for</strong>each(str<strong>in</strong>g s <strong>in</strong> theArray){writer.WriteStartElement("x", "element", null);writer.WriteBase64(Encod<strong>in</strong>g.Unicode.GetBytes(s),0, s.Length*2);writer.WriteEndElement();133

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

Saved successfully!

Ooh no, something went wrong!