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.

Limitations of the <strong>XML</strong> DOM Event<strong>in</strong>g ModelAlthough you receive notifications be<strong>for</strong>e and after an action takes place, you can't alterthe predef<strong>in</strong>ed flow of operations. In other words, you can per<strong>for</strong>m any action whilehandl<strong>in</strong>g the event, but you can't cancel the ongo<strong>in</strong>g operation. This also means thatyou can't just skip some nodes based on run-time conditions. In fact, the event handlerfunction is void, and all the arguments passed with the event data structure are readonly.Programmers have no way to pass <strong>in</strong><strong>for</strong>mation back to the reader and skip thecurrent node. There is only one way <strong>in</strong> which the event handler can affect the behaviorof the reader. If the event handler throws an exception, the reader will stop work<strong>in</strong>g. Inthis case, however, the <strong>XML</strong> DOM will not be built.Select<strong>in</strong>g Nodes by QueryAs mentioned, the <strong>XML</strong> DOM provides a few ways to traverse the document <strong>for</strong>est tolocate a particular node. The ChildNodes property returns a l<strong>in</strong>ked list <strong>for</strong>med by thechild nodes placed at the same level. You move back and <strong>for</strong>th <strong>in</strong> this list us<strong>in</strong>g theNextSibl<strong>in</strong>g and PreviousSibl<strong>in</strong>g methods.You can also enumerate the contents of the ChildNodes list us<strong>in</strong>g a <strong>for</strong>each-styleenumerator. This enumerator is built <strong>in</strong>to the XmlDocument class and returned ondemand by the GetEnumerator method, as shown here:<strong>for</strong>each(XmlNode n <strong>in</strong> node.ChildNodes){// Do someth<strong>in</strong>g}Direct Access to ElementsTheGetElementById method returns the first child node below the current node that hasan ID attribute with the specified value. Note that ID is a particular <strong>XML</strong> type and notsimply an attribute with that name. An attribute can be declared as an ID only <strong>in</strong> an<strong>XML</strong> Schema Def<strong>in</strong>ition (XSD) or a DTD schema. The follow<strong>in</strong>g <strong>XML</strong> fragment def<strong>in</strong>esan employeeid attribute of type ID. The attribute belongs to the Employee node.A correspond<strong>in</strong>g <strong>XML</strong> node might look like this:As you can see, the source <strong>XML</strong> is apparently unaffected by the use of an ID attribute.An ID attribute can be seen as an <strong>XML</strong> primary key, and the GetElementById method—part of the W3C DOM specification—represents the search method that applicationsuse to locate nodes. The follow<strong>in</strong>g code retrieves the node element <strong>in</strong> the documentwhose ID attribute (employeeid) matches the specified value:employeeNode = node.GetElementById("1");If you call GetElementById on a node whose children have no ID attributes or match<strong>in</strong>gvalues, the method returns null. The search <strong>for</strong> a match<strong>in</strong>g node stops when the firstmatch is found.Another query method at your disposal is GetElementsByTagName. As the namesuggests, this method returns a list of nodes with the specified name.GetElementsByTagName looks similar to ChildNodes but differs <strong>in</strong> one aspect.Whereas ChildNodes returns all the child nodes found, <strong>in</strong>clud<strong>in</strong>g all elements andleaves, GetElementsByTagName returns only the element nodes with a particularname. The name specified can be expressed as a local as well as a namespacequalifiedname.188

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

Saved successfully!

Ooh no, something went wrong!