Skip to content

31. Validate XML, SelectNodes(), XMLReader/SAX

March 26, 2011

91. How would you validate XML using .NET?
The XmlValidatingReader Class represents a reader that represents document type definition (DTD), XML Data Reduced (XDR) schema, and XML Schema definition language (XSD) validation. XmlValidatingReader implements the XmlReader class and provides support for data validation. Use the Schemas property to have the reader validate by using schema files that are cached in an XmlSchemaCollection. The ValidationType property specifies what type of validation the reader should perform. Setting the property to ValidationType.None creates a non validating reader. XmlValidatingReader throws an XmlException on Xml parse errors. After an exception is thrown the state of the reader is not predictable. For example, the reported node type may be different than the actual node type of the current node. Use the ReadState property to check whether a reader is in error state. Note that this is valid for .NET 1.x. As of .NET Version 2.0 to create a validating XmlReader instance use the XmlReaderSettings class and the Create method.  As of .NET 2.0 the XmlReader class can enforce validation using a DTD or XSD schema. Settings on the XmlReaderSettings object used by the Create method determine what type of data validation, the XmlReader supports.

92. Why is this almost always a bad idea? When is this a good idea? myXmlDocument.SelectNodes(“//mynode”);
Selects a list of nodes matching the XPath expression. In this case searches all nodes and children nodes. This can be overkill and cause bad performance since unnecessary searches are made. Better to use the full or relative path. Could be a good idea if all the mynode nodes wanted, for counting for example.

93. Describe the difference between pull-style parsers (XMLReader) and eventing-readers (Sax)
Like the Simple API for XML(SAX) reader, the XmlReader is a forward only, read only cursor. It provides fast, non cached stream access to the input. It can read a stream or a document. It allows the user to pull data and skip records of no interest to the application. The big difference lies in the fact that the SAX model is a push model, where the parser pushes events to the application, notifying the application every time a new node has been read, while applications using the XmlReader can pull nodes from the reader at will. The benefits of this pull model are in these areas:
State Management, the push model requires the content handlers to build very complex state machines. The pull model client simplifies state management by a natural, top down procedural refinement.
Multiple Input Streams, the pull model allows the client to put together multiple input streams. This is extremely complex to do in the push model.
Layering, the push model can be built on top of the pull model. The reverse is not true.
Extra String Copy Avoidance, normally the data is read from the parser buffer into the string object, which is then pushed to the client buffer. The pull model allows the client to give the parser a buffer into which the string is directly written.
Selective Processing, the push model notifies the client of each item, including attributes, processing instructions, and white space, while the pull model can skip items, processing only those items that are of interest to the application. This allows for extremely efficient applications. Also, properties can be set in advance that affect how the XML stream is processed(for example, normalization).

Happy Programming! =)
Source: mainly MSDN.


From → OOP

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: