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.

In particular, the XmlConvert class will not recognize the first str<strong>in</strong>g as a correct date.As <strong>for</strong> the salary, you'll get a message stat<strong>in</strong>g that the <strong>in</strong>put str<strong>in</strong>g is not <strong>in</strong> the correct<strong>for</strong>mat.If you had created the <strong>XML</strong> code programmatically us<strong>in</strong>g an <strong>XML</strong> writer (more on <strong>XML</strong>writers <strong>in</strong> Chapter 4) and .<strong>NET</strong> strong types, the <strong>XML</strong> fragment you're work<strong>in</strong>g withwould be slightly different, as shown here:2001-02-08150000To be understood <strong>in</strong> <strong>XML</strong>, a date must be <strong>in</strong> YYYY-MM-DD <strong>for</strong>mat and a double valueshould not <strong>in</strong>clude any locale-dependent element such as the digit group symbol. If thedouble value <strong>in</strong>cludes a fractional part, use a decimal po<strong>in</strong>t to separate it from the<strong>in</strong>teger part. Likewise, XmlConvert recognizes Booleans only if they are expressed astrue/false or 1/0 pairs.NoteAnother aspect that makes the difference between the System.Convert and XmlConvert classes even sharper is the fact thatXmlConvert does not support custom <strong>for</strong>mat providers. TheXmlConvert class works as a translator to and from .<strong>NET</strong> types andXSD types. When the conversion takes place, the result isrigorously locale <strong>in</strong>dependent.Round-Tripp<strong>in</strong>g Non-<strong>XML</strong> Str<strong>in</strong>gsNot all characters available on a given plat<strong>for</strong>m are necessarily valid <strong>XML</strong> characters.Only the characters <strong>in</strong>cluded <strong>in</strong> the range of allowed characters def<strong>in</strong>ed <strong>in</strong> the <strong>XML</strong>specification (www.w3.org/TR/2000/REC-xml-20001006.html) can be safely used <strong>for</strong>element and attribute names.The XmlConvert class provides key functions <strong>for</strong> tunnel<strong>in</strong>g non-<strong>XML</strong> names through<strong>XML</strong> over a round-trip to some servers. When names conta<strong>in</strong> characters that are <strong>in</strong>valid<strong>in</strong> <strong>XML</strong> names, the methods EncodeName and DecodeName can adjust them to fit <strong>in</strong>toan <strong>XML</strong> name schema. For example, several applications, <strong>in</strong>clud<strong>in</strong>g <strong>Microsoft</strong> SQLServer and <strong>Microsoft</strong> Office, allow and support Unicode characters <strong>in</strong> their documents.However, some of these characters are not valid <strong>in</strong> <strong>XML</strong> names. The typicalcircumstance that demonstrates the importance of XmlConvert occurs when youmanipulate, say, a database column name conta<strong>in</strong><strong>in</strong>g blanks. Although SQL Serverallows a column name such as Invoice Details, that would not be a valid name <strong>for</strong> an<strong>XML</strong> stream. The word space must be replaced with its hexadecimal encod<strong>in</strong>g. A valid<strong>XML</strong> representation <strong>for</strong> the column name Invoice Details is the follow<strong>in</strong>g str<strong>in</strong>g:Invoice_0x0020_DetailsYou can obta<strong>in</strong> that str<strong>in</strong>g by us<strong>in</strong>g EncodeName, as shown here:str<strong>in</strong>g xmlColName = XmlConvert.EncodeName("Invoice Details");The reverse operation is accomplished by us<strong>in</strong>g DecodeName. This method translatesan <strong>XML</strong> name back to its orig<strong>in</strong>al <strong>for</strong>m by unescap<strong>in</strong>g any escaped sequence, asshown <strong>in</strong> the follow<strong>in</strong>g code. Note that only fully escaped <strong>for</strong>ms are detected. Forexample, only _0x0020_ is rendered as a blank space.31

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

Saved successfully!

Ooh no, something went wrong!