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.

Table 5-2: Methods of the XmlDocument ClassMethodDescriptionwriter.As you can see, the XmlDocument class has a lot of methods that create and return<strong>in</strong>stances of node objects. In the .<strong>NET</strong> Framework, all the objects that represent a nodetype (Comment, Element, Attribute, and so on) do not have any publicly usableconstructors. For this reason, you must resort to the correspond<strong>in</strong>g method.How can the XmlDocument class create and return <strong>in</strong>stances of other node objects ifno public constructor <strong>for</strong> them is available? The trick is that node classes mark theirconstructors with the <strong>in</strong>ternal modifier (Friend <strong>in</strong> <strong>Microsoft</strong> Visual Basic). The <strong>in</strong>ternalkeyword restricts the default visibility of a type method or property to the boundaries ofthe assembly. The <strong>in</strong>ternal keyword works on top of other modifiers like public andprotected. XmlDocument and other node classes are all def<strong>in</strong>ed <strong>in</strong> the System.Xmlassembly, which ensures the effective work<strong>in</strong>g of factory methods. The follow<strong>in</strong>gpseudocode shows the <strong>in</strong>ternal architecture of a factory method:public virtual XmlXXX CreateXXX( params ){return new XmlXXX ( params );}NoteWhen the node class is XmlDocument, the methods WriteTo andWriteContentTo happen to produce the same output, although theydef<strong>in</strong>itely run different code. WriteTo is designed to persist the entirecontents of the node, <strong>in</strong>clud<strong>in</strong>g the markup <strong>for</strong> the node, attributes,and children. WriteContentTo, on the other hand, walks its waythrough the collection of child nodes and persists the contents ofeach us<strong>in</strong>g WriteTo. Here's the pseudocode:void WriteContentTo(XmlWriter w) {<strong>for</strong>each(XmlNode n <strong>in</strong> this)n.WriteTo(w);}A Document node is a k<strong>in</strong>d of super root node, so the loop on allchild nodes beg<strong>in</strong>s with the actual root node of the <strong>XML</strong> document.In this case, WriteTo simply writes out the entire contents of thedocument but the super root node has no markup. As a result, thetwo methods produce the same output <strong>for</strong> the XmlDocument class.Events of the XmlDocument ClassTable 5-3 lists the events that the XmlDocument class fires under the follow<strong>in</strong>g specificconditions: when the value of a node (any node) is be<strong>in</strong>g edited, and when a node isbe<strong>in</strong>g <strong>in</strong>serted <strong>in</strong>to or removed from the document.Table 5-3: Events of the XmlDocument ClassEventsNodeChang<strong>in</strong>g,NodeChangedNodeInsert<strong>in</strong>g,NodeInsertedDescriptionThe Value property of a node belong<strong>in</strong>g to thisdocument is about to be changed or has beenchanged already.A node is about to be <strong>in</strong>serted <strong>in</strong>to another node175

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

Saved successfully!

Ooh no, something went wrong!