Sunday, June 6, 2010

XPathDocument vs XmlDocument

XmlDocument
XPathDocument
- Based on W3C DOM Model
- Loads entire Xml in memory
- Read/Write
- Slower than XPathDocument
- Support for XPath/XSLT
- Supports both XPathNavigator and DOM interfaces
- Based on XPath Data Model
- Loads entire Xml in memory
- Read Only
- Faster than XmlDocument
- Optimized support for XPath/XSLT
- Supports only XPathNavigator interfaces


XmlDocument is based on the W3C XML DOM, which is an object model that basically covers all XML syntaxes, including low-level syntax sugar such as entities, CDATA sections, DTD, notations, etc. That's a document-centric model and it allows for full fidelity when loading/saving XML documents.

XPathDocument is based on an XPath 1.0 data model that is a read-only XML Infoset-compatible data-centric object model that covers only semantically significant parts of XML, leaving out insignificant syntax details - no DTD, no entities, no CDATA, no adjacent text nodes, only significant data expressed as a tree with seven types of nodes.

Simple and lightweight. That's why XPathDocument is a preferred data store for read-only scenarios, especially with XPath or XSLT involved.

Saturday, June 5, 2010

Reading an Xml

1. Parsing the XML 1.0 byte stream

a. XmlTextReader : Parses the XML 1.0 Byte Stream and the complexities of the XML 1.0 syntax by serving up the document as a logical-tree structure through higher-level APIs.

- Performance
- Memory
- Traversal
- Operation
- XPath
- XSLT
- Fastest
- Most efficient as only one node needs to be in memory
- Forward Only
- Read Only
- No
- No

2. Processing the Logical Tree via XML APIs

i. Streaming

a. XmlReader

- Models read an Xml as a forward-only, linear stream of nodes.
- XmlReader allows the client to pull the nodes one at a time much like the firehose cursor model in data access technology.


ii. Traversal Oriented

a. XmlNode (XmlDocument)

- Built on top of XmlReader


- Performance
- Memory
- Traversal
- Operation
- XPath
- XSLT
- 2 to 3x slower than XmlTextReader
- Loads entire Xml/Tree Structure in Memory
- Full Traversal
- Read/Write
- Yes
- No

b. XPathNavigator

- Uses a cursor model, which gives the underlying implementation more options in terms of how the tree is actually stored.

- Performance
- Memory
- Traversal
- Operation
- XPath
- XSLT
- Faster than XmlDocument
- More efficient than XmlDocument
- Full Traversal
- Read Only
- Yes
- Yes

3. Choosing which class


  • What kind of reader should I use?
    Use XmlTextReader if:
    * Performance is your highest priority and…
    * You don't need XSD/DTD validation and…
    * You don't need XSD type information at runtime and…
    * You don't need XPath/XSLT services

    Use XmlValidatingReader if:

    * You need XSD/DTD validation or…
    * You need XSD type information at runtime or…
  • Should I load the tree into memory?

    Use the DOM if:

    * Productivity is your highest priority or…
    * You need XPath services or…
    * You need to update the document (read/write)
  • XmlDocument or XPathDocument?
    * You need to execute an XSLT transformation or…
    * You want to leverage an implementation (like XPathDocument)

Sunday, May 30, 2010

Thread Synchronization

Threads need to communicate with each other in two basic situations:

1. When you have multiple threads accessing a shared resource in such a way that the resource does not become corrupt

2. When one thread needs to notify one or more other threads that a specific task has been completed

User Mode

1. Interlocked Methods

a. These methods are extremely fast and easy to use.

b. The System.Threading.Interlocked class defines a bunch of static methods that can atomically modify a variable in a thread-safe way.

 public static class Interlocked {
// Atomically performs (location++)
public static Int32 Increment(ref Int32 location);
// Atomically performs (location--)
public static Int32 Decrement(ref Int32 location);
// Atomically performs (location1 += value)
// Note: value can be a negative number allowing subtraction
public static Int32 Add(ref Int32 location1, Int32 value);
// Atomically performs (location1 = value)
public static Int32 Exchange(ref Int32 location1, Int32 value);
// Atomically performs the following:
// if (location1 == comparand) location1 = value
public static Int32 CompareExchange(ref Int32 location1,
Int32 value, Int32 comparand);
}

2. Critical Sections

A critical section is a small section of code that requires exclusive access to some shared resource before the code can execute.

Implemented in .NET using the Monitor class.

 private void SomeMethod() {
lock (this) {
// Access object here...
}
}
private void SomeMethod() {
Object temp = this;
Monitor.Enter(temp);
try {
// Access object here...
}
finally {
Monitor.Exit(temp);
}
}

3. ReaderWriterLock

There is a very common thread synchronization problem known as the multiple-reader/
single-writer problem.

a. When one thread is writing to the data, no other thread can write to the data.
b. When one thread is writing to the data, no other thread can read from the data.
c. When one thread is reading from the data, no other thread can write to the data.
d. When one thread is reading from the data, other threads can also read from the data.

Should never ever use the class.

Note : Monitor and ReaderWriterLock methods allow synchronization of threads running
only in a single AppDomain.

Kernel Mode

1. Kernel objects can be used to synchronize threads that are running in different AppDomains or in different processes.

2. Whenever a thread waits on a kernel object, the thread must always transition from user mode to kernel mode (1000 CPU cycles), causing the thread to incur a performance hit.

3. WaitHandle class is a simple class whose sole purpose is to wrap a Windows kernel object.

.NET Roadmap


Component200220032004200520062007200820092010
.NET1.01.12.03.03.53.5 SP14.0
Visual StudioVS.NETVS.NET 2003VS.NET 2005VS.NET 2008VS.NET 2010