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.

str<strong>in</strong>g colName =XmlConvert.DecodeName("Invoice_0x0020_Details");The only valid <strong>for</strong>m of hexadecimal sequences is _0xHHHH_, where HHHH stands <strong>for</strong>a four-digit hexadecimal value. Similar <strong>for</strong>ms are left unaltered, although they couldeasily be considered logically equivalent—<strong>for</strong> example, _0x20_ is not processed.Character Encod<strong>in</strong>g<strong>XML</strong> documents can conta<strong>in</strong> an attribute to specify the encod<strong>in</strong>g. Character encod<strong>in</strong>gprovides a mapp<strong>in</strong>g between numeric <strong>in</strong>dexes and correspond<strong>in</strong>g characters that usersread from a document. The follow<strong>in</strong>g declaration shows how to set the requiredencod<strong>in</strong>g <strong>for</strong> an <strong>XML</strong> document:The Encod<strong>in</strong>g property of the <strong>XML</strong> reader returns the character encod<strong>in</strong>g found <strong>in</strong> thedocument. The default encod<strong>in</strong>g attribute is UTF-8 (UCS Trans<strong>for</strong>mation Format, 8bits).In the .<strong>NET</strong> Framework, the System.Text.Encod<strong>in</strong>g class gathers all supportedencod<strong>in</strong>gs. Most of these encod<strong>in</strong>gs can be used with <strong>XML</strong> documents, with just a fewexceptions. Encod<strong>in</strong>gs such as UTF-7 are <strong>in</strong>valid <strong>for</strong> <strong>XML</strong> documents because theyrequire different byte values than UTF-8. UTF-8 encodes Unicode characters us<strong>in</strong>g 8bits per character. UTF-7, on the other hand, encodes Unicode characters us<strong>in</strong>g 7 bitsper character.Access<strong>in</strong>g AttributesOf all the node types supplied <strong>in</strong> the .<strong>NET</strong> Framework, only Element, DocumentType,and XmlDeclaration support attributes. To check whether a given node conta<strong>in</strong>sattributes, use the HasAttributes Boolean property. The AttributeCount property returnsthe number of attributes available <strong>for</strong> the current node.Once the <strong>in</strong>ternal reader's po<strong>in</strong>ter is positioned on a certa<strong>in</strong> node, you can directly readthe value of a particular attribute us<strong>in</strong>g either the GetAttribute method or the <strong>in</strong>dexerproperty Item. In both cases, overloads of the method and the property allow you toaccess attributes <strong>in</strong> various ways: by absolute position, by name, and by name andnamespace. The returned value <strong>for</strong> an attribute is always a str<strong>in</strong>g; the task of convert<strong>in</strong>git to a more specific data type is left to the programmer.GetAttribute and Item provide a way to access attributes directly but require that youknow the name or the ord<strong>in</strong>al position of the attribute be<strong>in</strong>g accessed. A third way toread attribute values is by mov<strong>in</strong>g the po<strong>in</strong>ter to the attribute node itself and then us<strong>in</strong>gthe Value property. You enumerate the attribute nodes us<strong>in</strong>g the MoveToFirstAttributeand MoveToNextAttribute methods. You can also change the po<strong>in</strong>ter by mov<strong>in</strong>g directlyto a given node us<strong>in</strong>g the MoveToAttribute method.This next example demonstrates how to programmatically access any sequence ofattributes <strong>for</strong> a node and concatenate their names and values <strong>in</strong> a s<strong>in</strong>gle str<strong>in</strong>g.Consider the follow<strong>in</strong>g <strong>XML</strong> fragment:We want to create a method that, when run on this <strong>XML</strong> block of data, generates thefollow<strong>in</strong>g str<strong>in</strong>g:id="1" lastname="Users" firstname="Joe"32

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

Saved successfully!

Ooh no, something went wrong!