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.

stops the load<strong>in</strong>g. Otherwise, the operation cont<strong>in</strong>ues unless the handler itself throwsan exception.Load<strong>in</strong>g from a Str<strong>in</strong>gThe <strong>XML</strong> DOM programm<strong>in</strong>g <strong>in</strong>terface also provides you with a method to build a DOMfrom a well-<strong>for</strong>med <strong>XML</strong> str<strong>in</strong>g. The method is LoadXml and is shown here:public virtual void LoadXml(str<strong>in</strong>g xml);This method neither supports validation nor preserves white spaces. Any contextspecific<strong>in</strong><strong>for</strong>mation you might need (DTD, entities, namespaces) must necessarily beembedded <strong>in</strong> the str<strong>in</strong>g to be taken <strong>in</strong>to account.Load<strong>in</strong>g Documents AsynchronouslyThe .<strong>NET</strong> Framework implementation of the <strong>XML</strong> DOM does not provide <strong>for</strong>asynchronous load<strong>in</strong>g. The Load method, <strong>in</strong> fact, always work synchronously and doesnot pass the control back to the caller until completed. As you might guess, this canbecome a serious problem when you have huge files to process and a rich user<strong>in</strong>terface.In similar situations—that is, when you are writ<strong>in</strong>g a W<strong>in</strong>dows Forms rich client—us<strong>in</strong>gthreads can be the most effective solution. You transfer to a worker thread the burdenof load<strong>in</strong>g the <strong>XML</strong> document and update the user <strong>in</strong>terface when the thread returns, asshown here:void StartDocumentLoad<strong>in</strong>g(){// Create the worker threadThread t = new Thread(newThreadStart(this.LoadXmlDocument));}statusBar.Text = "Load<strong>in</strong>g document...";t.Start();void LoadXmlDocument(){XmlDocument doc = new XmlDocument();doc.Load(InputFile.Text);// Update the user <strong>in</strong>terfacestatusBar.Text = "Document loaded.";Output.Text = doc.OuterXml;Output.ReadOnly = false;}return;185

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

Saved successfully!

Ooh no, something went wrong!