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.

Consider the follow<strong>in</strong>g attribute value, <strong>in</strong> which the entity character <br />

denotes al<strong>in</strong>efeed character:Let's try to read the AuthorDisplayName attribute us<strong>in</strong>g the XmlTextReader parserwhen the normalization is off. The follow<strong>in</strong>g code shows how:reader.Normalization = false;reader.Read();Console.WriteL<strong>in</strong>e(reader["AuthorDisplayName"]);In the result<strong>in</strong>g str<strong>in</strong>g, the l<strong>in</strong>efeed is preserved, and the output <strong>in</strong> the console w<strong>in</strong>dowlooks like this:D<strong>in</strong>oEspositoConversely, if you read the attribute when Normalization is set to true, the l<strong>in</strong>e-feed isreplaced with a blank, and the output looks like this:D<strong>in</strong>o EspositoHandl<strong>in</strong>g <strong>XML</strong> ExceptionsThe <strong>XML</strong> reader throws an exception whenever it encounters a pars<strong>in</strong>g error <strong>in</strong> the<strong>XML</strong> source. The reader makes use of the XmlException class to return detailed<strong>in</strong><strong>for</strong>mation about the last pars<strong>in</strong>g error. Ad hoc <strong>in</strong><strong>for</strong>mation <strong>in</strong>cludes the l<strong>in</strong>e number,the character position, and a text description. L<strong>in</strong>ePosition and L<strong>in</strong>eNumber, shownhere, are the members that differentiate the XmlException class from the basic .<strong>NET</strong>Exception class:public class XmlException : SystemException{<strong>in</strong>t L<strong>in</strong>ePosition;<strong>in</strong>t L<strong>in</strong>eNumber;}Although you can still catch <strong>XML</strong> pars<strong>in</strong>g and validation exceptions through the basicException class, catch<strong>in</strong>g them through XmlException gives you more <strong>in</strong><strong>for</strong>mation andthe certa<strong>in</strong>ty that the error relates only to the code handl<strong>in</strong>g <strong>XML</strong> data.NoteIf you have multiple <strong>XML</strong> documents <strong>in</strong> a s<strong>in</strong>gle stream to parse <strong>in</strong>sequence, you can still use the same <strong>in</strong>stance of the reader.However, prior to attack<strong>in</strong>g a new stream, you must reset the<strong>in</strong>ternal state of the reader. The XmlTextReader class specificallydef<strong>in</strong>es a method, named ResetState, that simply resets the state ofthe reader to ReadState.Initial.ResetState resets all the properties to their default values, with afew exceptions. Normalization, XmlResolver, andWhitespaceHandl<strong>in</strong>g are not affected by the state reset.Handl<strong>in</strong>g White SpacesIn <strong>XML</strong>, white spaces are a special type of node. White spaces found <strong>in</strong> the body of an<strong>XML</strong> document can be classified <strong>in</strong> two groups: significant and <strong>in</strong>significant. A white35

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

Saved successfully!

Ooh no, something went wrong!