Wednesday, November 16, 2011

Multi Threading


What is a thread?

Every application runs with at least one thread.  So what is a thread?  A thread is nothing more than a process.  My guess is that the word thread comes from the Greek mythology of supernatural Muses weaving threads on a loom, where each thread represents a path in time of someone's life.  If you mess with that thread, then you disturb the fabric of life or change the process of life.  On the computer, a thread is a process moving through time.  The process performs sets of sequential steps, each step executing a line of code.  Because the steps are sequential, each step takes a given amount of time.  The time it takes to complete a series of steps is the sum of the time it takes to perform each programming step.

What are multithreaded applications?

For a long time, most programming applications (except for embedded system programs) were single-threaded.  That means there was only one thread in the entire application.  You could never do computation A until completing computation B.  A program starts at step 1 and continues sequentially (step 2, step 3, step 4) until it hits the final step (call it step 10).   A multithreaded application allows you to run several threads, each thread running in its own process.  So theoretically you can run step 1 in one thread and at the same time run step 2 in another thread.  At the same time you could run step 3 in its own thread, and even step 4 in its own thread.  Hence step 1, step 2, step 3, and step 4 would run concurrently.  Theoretically, if all four steps took about the same time, you could finish your program in a quarter of the time it takes to run a single thread (assuming you had a 4 processor machine).  So why isn't every program multithreaded?  Because along with speed, you face complexity.  Imagine if step 1 somehow depends on the information in step 2.  The program might not run correctly if step 1 finishes calculating before step 2 or visa versa.

An Unusual Analogy

Another way to think of multiple threading is to consider the human body.  Each one of the body's organs (heart, lungs, liver, brain) are all involved in processes.  Each process is running simultaneously.  Imagine if each organ ran as a step in a process: first the heart, then the brain, then the liver, then the lungs.  We would probably drop dead.   So the human body is like one big multithreaded application.  All organs are processes running simultaneously, and all of these processes depend upon one another.  All of these processes communicate through nerve signals, blood flow and chemical triggers.  As with all multithreaded applications, the human body is very complex.  If some processes don't get information from other processes, or certain processes slow down or speed up, we end up with a medical problem.  That's why (as with all multithreaded applications) these processes need to be synchronized properly in order to function normally.

When to Thread

Multiple threading is most often used in situations where you want programs to run more efficiently.  For example, let's say your Window Form program contains a method (call it method_A) inside it that takes more than a second to run and needs to run repetitively.  Well, if the entire program ran in a single thread,  you would notice times when button presses didn't work correctly, or your typing was a bit sluggish.  If method_A was computationally intensive enough, you might even notice certain parts of your Window Form not working at all.  This unacceptable program behavior is a sure sign that you need multithreading in your program.  Another common scenario where you would need threading is in a messaging system.  If you have numerous messages being sent into your application,  you need to capture them at the same time your main processing program is running and distribute them appropriately.  You can't efficiently capture a series of messages at the same time you are doing any heavy processing, because otherwise you may miss messages.  Multiple threading can also be used in an assembly line fashion where several processes run simultaneously.  For example once process collects data in a thread, one process filters the data, and one process matches the data against a database.  Each of these scenarios are common uses for multithreading and will significantly improve performance of similar applications running in a single thread.

When not to Thread

It is possible that when a beginning programmer first learns threading, they may be fascinated with the possibility of using threading in their program.  They may actually become thread-happy.  Let me elaborate:
Day 1) Programmer learns the that they can spawn a thread and begins creating a single new thread in their program, Cool!
Day 2) Programmer says,  " I can make this even more efficient by spawning other threads in parts of my program!"
Day 3)  P:  "Wow, I can even fork threads within threads and REALLY improve efficiency!!"
Day 4)  P: "I seem to be getting some odd results, but that's okay, we'll just ignore them for now."
Day 5)  "Hmmmm, sometimes my widgetX variable has a value,  but other times it never seems to get set at all, must be my computer isn't working, I'll just run the debugger".
Day 9)  "This darn (stronger language) program is jumping all over the place!!  I can't figure out what is going on!"
Week 2)  Sometimes the program just sits there and does absolutely nothing!  H-E-L-P!!!!!
Sound familiar?  Almost anyone who has attempted to design a multi-threaded program for the first time, even with good design knowledge of threading, has probably experienced at least 1 or 2 of these daily bullet points.  I am not insinuating that threading is a bad thing,   I'm just pointing out that in the process of creating threading efficiency in your programs, be very, very careful.   Because unlike a single threaded program, you are handling many processes at the same time, and multiple processes, with multiple dependent variables, can be very tricky to follow. Think of multithreading like you would think of juggling.  Juggling a single ball in your hand (although kind of boring)  is fairly simple.  However, if you are challenged to put two of those balls in the air, the task is a bit more difficult.  3, 4, and 5, balls are progressively more difficult.  As the ball count increases, you have a better and better chance of really dropping the ball.  Juggling a lot of balls at once requires knowledge, skill, and precise timing.  So does multiple threading.


Problems with Threading

If every process in your program was mutually exclusive - that is, no process depended in any way upon another, then multiple threading would be very easy and very few problems would occur.  Each process would run along in its own happy course and not bother the other processes.  However, when more than one process needs to read or write the memory used by other processes, problems can occur.  For example let's say there are two processes, process #1 and process #2.  Both processes share variable X.  If thread process #1 writes variable X with the value 5 first and thread process #2 writes variable X with value -3 next, the final value of X is -3.  However if process #2 writes variable X with value -3 first and then process #1 writes variable X with value 5, the final value of  X is 5.  So you see,  if the process that allows you to set X has no knowledge of process #1 or process #2, X can end up with different final values depending upon which thread got to X first.  In a single threaded program, there is no way this could happen, because everything follows in sequence.  In a single threaded program, since no processes are running in parallel,  X is always set by method #1 first, (if it is called first) and then set by method #2.  There are no surprises in a single threaded program, it's just step by step.    With a mulithreaded program, two threads can enter a piece of code at the same time, and wreak havoc on the results.  The problem with threads is that you need some way to control one thread accessing a shared piece of memory while another thread running at the same time is allowed to enter the same code and manipulate the shared data.

Thread Safety

Imagine if every time you juggled three balls,  the ball in the air, by some freak of nature, was never allowed to reach your right hand until the ball already sitting in your right hand was thrown.  Boy, would juggling be a lot easier!  This is what thread safety is all about. In our program we force one thread to wait inside our code block while the other thread is finishing its business.  This activity, known as thread blocking or synchronizing threads, allows us to control the timing of simultaneous threads running inside our program.  In C# we lock on a particular part of memory (usually an instance of an object) and don't allow any thread to enter code of this object's memory until another thread is done using the object.  By now you are probably thirsting for a code example, so here you  go.
Let's take a look at a two-threaded scenario.   In our example, we will create two threads in C#: Thread 1 and Thread 2, both running in their own while loop.  The threads won't do anything useful, they will just print out a message saying which thread they are part of.  We will utilize a shared memory class member called _threadOutput.  _threadOutput will be assigned a message based upon the thread in which it is running.  Listing #1 shows the two threads contained in DisplayThread1 and DisplayThread2 respectively.
Listing 1 - Creating two threads sharing a common variable in memory
            // shared memory variable between the two threads
            // used to indicate which thread we are in
            private string _threadOutput = "";
            /// <summary>            /// Thread 1: Loop continuously,
            /// Thread 1: Displays that we are in thread 1            /// </summary>            void DisplayThread1()
            {
                  while (_stopThreads == false)
                  {
                        Console.WriteLine("Display Thread 1");
                        // Assign the shared memory to a message about thread #1
                        _threadOutput = "Hello Thread1";

                        Thread.Sleep(1000);  // simulate a lot of processing
 
                        // tell the user what thread we are in thread #1, and display shared memory                        Console.WriteLine("Thread 1 Output --> {0}", _threadOutput);

                  }
            }

            /// <summary>
            /// Thread 2: Loop continuously,

            /// Thread 2: Displays that we are in thread 2
            /// </summary>            void DisplayThread2()
            {
                  while (_stopThreads == false)
                  {
                    Console.WriteLine("Display Thread 2");
 
                   // Assign the shared memory to a message about thread #2
                    _threadOutput = "Hello Thread2";

                    Thread.Sleep(1000);  // simulate a lot of processing

 
                  // tell the user we are in thread #2                    Console.WriteLine("Thread 2 Output --> {0}", _threadOutput);
                  }
            }

            Class1()
            {
                  // construct two threads for our demonstration;                  Thread thread1 = new Thread(new ThreadStart(DisplayThread1));
                  Thread thread2 = new Thread(new ThreadStart(DisplayThread2));

                  // start them                  thread1.Start();
                  thread2.Start();
            }
The results of this code is shown in figure 2.  Look carefully at the results.  You will notice that the program gives some surprising output(if we were looking at this from a single-threaded mindset).  Although we clearly assigned _threadOutput to a string with a number corresponding to the thread to which it belongs, that is not what it looks like in the console:
We would expect to see the following from our code
 Thread  1 Output --> Hello Thread 1   and  Thread 2 Output --> Hello Thread 2,   but for the most part, the results are completely unpredictable.
Sometimes we see   Thread  2 Output --> Hello Thread 1   and  Thread 1 Output --> Hello Thread 2.   The thread output does not match the code!  Even though, we look at the code and follow it with our eyes,  _threadOutput = "Hello Thread 2",   Sleep,    Write "Thread 2 -->  Hello Thread 2",  this sequence we expect does not necessarily produce the final result.

Explanation

The reason we see the results we do is because in a multithreaded program such as this one, the code theoretically is executing the two methods DisplayThread1 and DisplayThread2, simultaneously. Each method shares the variable, _threadOutput.  So it is possible that although _threadOutput is assigned a value "Hello Thread1" in thread #1 and displays _threadOutput two lines later to the console, that somewhere in between the time thread #1 assigns it and displays it, thread #2 assigns _threadOutput the value  "Hello Thread2".  Not only are these strange results, possible, they are quite frequent as seen in the output shown in figure 2.  This painful threading problem is an all too common bug in thread programming known as a race condition.  This example is a very simple example of the well-known threading problem.  It is possible for this problem to be hidden from the programmer much more indirectly such as through referenced variables or collections pointing to thread-unsafe variables.   Although in figure 2 the symptoms are blatant, a race condition can appear much more rarely and be intermittent once a minute, once an hour, or appear three days later.  The race is probably the programmer's worst nightmare because of its infrequency and because it can be very very hard to reproduce.

Winning the Race

The best way to avoid race conditions is to write thread-safe code.  If your code is thread-safe,  you can prevent some nasty threading issues from cropping up.  There are several defenses for writing thread-safe code.  One is to share memory as little as possible.  If you create an instance of a class, and it runs in one thread,  and then you create another instance of the same class, and it runs in another thread, the classes are thread safe as long as they don't contain any static variables.  The two classes each create their own memory for their own fields, hence no shared memory.    If you do have static variables in your class or the instance of your class is shared by several other threads,  then you must find a way to make sure one thread cannot use the memory of that variable until the other class is done using it.    The way we prevent one thread from effecting the memory of the other class while one is occupied with that memory is calledlocking.  C# allows us to lock our code with either a Monitor class or a lock { }  construct.   (The lock construct actually internally implements the Monitor class through a try-finally block, but it hides these details from the programmer).  In our example in listing 1,  we can lock the sections of code from the point in which we populate the shared _threadOutput variable all the way to the actual output to the console.  We lock our critical section of code in both threads so we don't have a race in one or the other.  The quickest and dirtiest way to lock inside a method is to lock on the this pointer.  Locking on the this pointer will lock on the entire class instance, so any thread trying to modify a field of the class while inside the lock, will be blocked.  Blocking means that the thread trying to change the variable will sit and wait until the lock is released on the locked thread.   The thread is released from the lock upon reaching the last bracket in the lock { } construct.
Listing 2 - Synchronizing two Threads by locking them
           /// <summary>           /// Thread 1, Displays that we are in thread 1 (locked)            /// </summary>            void DisplayThread1()
            {
                  while (_stopThreads == false)
                  {
                      
// lock on the current instance of the class for thread #1
                        lock (this)
                        {
                              Console.WriteLine("Display Thread 1");
                              _threadOutput = "Hello Thread1";
                              Thread.Sleep(1000);  // simulate a lot of processing                              // tell the user what thread we are in thread #1                              Console.WriteLine("Thread 1 Output --> {0}", _threadOutput);
                        } 
// lock released  for thread #1 here
                  }
            }

           /// <summary>           /// Thread 1, Displays that we are in thread 1 (locked)            /// </summary>            void DisplayThread2()
            {
                  while (_stopThreads == false)
                  {
                      // lock on the current instance of the class for thread #2
                        lock (this)
                        {
                              Console.WriteLine("Display Thread 2");
                              _threadOutput = "Hello Thread2";
                              Thread.Sleep(1000);  // simulate a lot of processing                              // tell the user what thread we are in thread #1                              Console.WriteLine("Thread 2 Output --> {0}", _threadOutput);
                        }  
// lock released  for thread #2 here
                  }
            }
The results of locking the two threads is shown in figure 3.  Note that all thread output is nicely synchronized.  You always get a result saying   Thread  1 Output --> Hello Thread 1   and  Thread 2 Output --> Hello Thread 2.   Note, however, that thread locking does come at a price.  When you lock a thread,  you force the other thread to wait until the lock is released.  In essence, you've slowed down the program, because while the other thread is waiting to use the shared memory, the first thread isn't doing anything in the program.  Therefore you need to use locks sparingly; don't just go and lock every method you have in your code if they are not involved in shared memory.  Also be careful when you use locks, because you don't want to get into the situation where thread #1 is waiting for a lock to be released by thread #2,  and thread #2 is waiting for a lock to be released by thread #1. When this situation happens, both threads are blocked and the program appears frozen.  This situation is known as deadlock and is almost as bad a situation as a race condition, because it can also happen at unpredictable, intermittent periods in the program.

Alternative Solution

.NET provides many mechanisms to help you control threads.  Another way to keep a thread blocked while another thread is processing a piece of shared memory is to use an AutoResetEvent.  The AutoResetEvent class has two methods,  Setand WaitOne.  These two methods can be used together for controlling the blocking of a thread.  When an AutoResetEvent is initialized with false, the program will stop at the line of code that calls WaitOne until the Set method is called on the AutoResetEvent.  After the Set method is executed on the AutoResetEvent,  the thread becomes unblocked and is allowed to proceed past WaitOne.   The next time WaitOne is called, it has automatically been reset, so the program will again wait (block) at the line of code in which the WaitOne method is executing.  You can use this "stop and trigger" mechanism to block on one thread until another thread is ready to free the blocked thread by calling Set.  Listing 3 shows our same two threads using the AutoResetEvent to block each other while the block thread waits and the unblocked thread executes to display _threadOutput to the Console.  Initially, _blockThread1 is initialized to signal false, while _blockThread2  is initialized to signal true.  This means that _blockThread2 will be allowed to proceed through the WaitOne call the first time through the loop in DisplayThread_2,  while _blockThread1 will block on its WaitOne call in DisplayThread_1.   When the _blockThread2 reaches the end of the loop in thread 2, it signals _blockThread1 by calling Set in order to release thread 1 from its block.  Thread 2 then waits in its WaitOne call until Thread 1 reaches the end of its loop and calls Set on _blockThread2.  The Set called in Thread 1 releases the block on thread 2 and the process starts again.   Note that if we had set both AutoResetEvents (_blockThread1 and _blockThread2)  initially to signal false, then both threads would be waiting to proceed through the loop without any chance to trigger each other, and we would experience a deadlock. 
Listing 3 - Alternatively Blocking threads with the AutoResetEvent
            AutoResetEvent _blockThread1 = new AutoResetEvent(false);
            AutoResetEvent _blockThread2 = new AutoResetEvent(true);
            /// <summary>            /// Thread 1, Displays that we are in thread 1            /// </summary>
            void DisplayThread_1()
            {
                  while (_stopThreads == false)
                  {
                           // block thread 1  while the thread 2 is executing                            _blockThread1.WaitOne(); 
                            // Set was called to free the block on thread 1, continue executing the code                              Console.WriteLine("Display Thread 1");
                              _threadOutput = "Hello Thread 1";
                              Thread.Sleep(1000);  // simulate a lot of processing

 
                              // tell the user what thread we are in thread #1                              Console.WriteLine("Thread 1 Output --> {0}", _threadOutput);
                            // finished executing the code in thread 1, so unblock thread 2                              _blockThread2.Set();
                  }
            }
            /// <summary>            /// Thread 2, Displays that we are in thread 2            /// </summary>            void DisplayThread_2()
            {
                  while (_stopThreads == false)
                  {
                        // block thread 2  while thread 1 is executing                              _blockThread2.WaitOne(); 
                        // Set was called to free the block on thread 2, continue executing the code                              Console.WriteLine("Display Thread 2");
                              _threadOutput = "Hello Thread 2";
                              Thread.Sleep(1000);  // simulate a lot of processing
                               // tell the user we are in thread #2                              Console.WriteLine("Thread 2 Output --> {0}", _threadOutput); 
                        // finished executing the code in thread 2, so unblock thread 1                            _blockThread1.Set();
                  }
            }
The output produced by listing 3  is the same output as our locking code, shown in figure 3,  but the AutoResetEvent gives us some more dynamic control over how one thread can notify another thread when the current thread is done processing.

C# Regular Expression


Description 


The source code shows how to use Regular Expressions in C#. The code Functions written for Validation Alphabet, AlphaNumeric, Integer, Postive Integer, Floating point numbers. You just cut copy these functions and use in any program.

Explanation of Regular Expressions:

Regular expressions are used to search specified in the source string.
Examples:
Pattern#1
Regex objNotNaturalPattern=new Regex("[^0-9]");

Pattern#2
Regex objNaturalPattern=new Regex("0*[1-9][0-9]*"); 
Pattern#1 will match for strings other than 0 to 9.^ symbol is used for Specifying not condition.[] brackets if we are to give range values such as 0 - 9 or a-z or A-Z eg. abc will return true
123 will return false.   
Pattern#2 will match for string which are Natural Numbers.Natural numbers Are numbers which are always greater than 0.The pattern 0* tells that a natural Number can be prefixed with any number of zero's or no zero's.the next [1-9] tells that it should contain atleast one number from 1 to 9 followed by any numbers of
0-9's 
Eg. 0007 returns true whereas 00 will return false. 
Basic things to be understood in RegEx: 
"*" matches 0 or more patterns
"?" matches single character
"^" for ignoring matches.
"[]" for searching range patterns.
More RegEx patterns in Next Article. 

Source Code:

// Source Code startsusing System.Text.RegularExpressions;using System;/*
<HowToCompile>
csc /r:System.Text.RegularExpressions.dll,System.dll Validation.cs
</HowToComplie>
*/
 class Validation
public static void Main()
{
String strToTest;
Validation objValidate=
new Validation();
Console.Write("Enter a String to Test for Alphabets:");
strToTest=Console.ReadLine();
if(objValidate.IsAlpha(strToTest))
{
Console.WriteLine("{0} is Valid Alpha String",strToTest);
}
else{
Console.WriteLine("{0} is not a Valid Alpha String",strToTest);
}
// Function to test for Positive Integers. public bool IsNaturalNumber(String strNumber)
{
Regex objNotNaturalPattern=
new Regex("[^0-9]");
Regex objNaturalPattern=
new Regex("0*[1-9][0-9]*");return !objNotNaturalPattern.IsMatch(strNumber) &&
objNaturalPattern.IsMatch(strNumber);
// Function to test for Positive Integers with zero inclusive public bool IsWholeNumber(String strNumber)
{
Regex objNotWholePattern=
new Regex("[^0-9]");return !objNotWholePattern.IsMatch(strNumber);
// Function to Test for Integers both Positive & Negative public bool IsInteger(String strNumber)
{
Regex objNotIntPattern=
new Regex("[^0-9-]");
Regex objIntPattern=
new Regex("^-[0-9]+$|^[0-9]+$");return !objNotIntPattern.IsMatch(strNumber) && objIntPattern.IsMatch(strNumber);
// Function to Test for Positive Number both Integer & Real public bool IsPositiveNumber(String strNumber)
{
Regex objNotPositivePattern=
new Regex("[^0-9.]");
Regex objPositivePattern=
new Regex("^[.][0-9]+$|[0-9]*[.]*[0-9]+$");
Regex objTwoDotPattern=
new Regex("[0-9]*[.][0-9]*[.][0-9]*");return !objNotPositivePattern.IsMatch(strNumber) &&
objPositivePattern.IsMatch(strNumber) &&
!objTwoDotPattern.IsMatch(strNumber);
// Function to test whether the string is valid number or notpublic bool IsNumber(String strNumber)
{
Regex objNotNumberPattern=
new Regex("[^0-9.-]");
Regex objTwoDotPattern=
new Regex("[0-9]*[.][0-9]*[.][0-9]*");
Regex objTwoMinusPattern=
new Regex("[0-9]*[-][0-9]*[-][0-9]*");
String strValidRealPattern="^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
String strValidIntegerPattern="^([-]|[0-9])[0-9]*$";
Regex objNumberPattern =
new Regex("(" + strValidRealPattern +")|(" + strValidIntegerPattern + ")");return !objNotNumberPattern.IsMatch(strNumber) &&
!objTwoDotPattern.IsMatch(strNumber) &&
!objTwoMinusPattern.IsMatch(strNumber) &&
objNumberPattern.IsMatch(strNumber);
// Function To test for Alphabets. public bool IsAlpha(String strToCheck)
{
Regex objAlphaPattern=
new Regex("[^a-zA-Z]");return !objAlphaPattern.IsMatch(strToCheck);
}
// Function to Check for AlphaNumeric.public bool IsAlphaNumeric(String strToCheck)
{
Regex objAlphaNumericPattern=
new Regex("[^a-zA-Z0-9]");return !objAlphaNumericPattern.IsMatch(strToCheck);
}
// Source Code End

Using XML


First, I will discuss XML .NET Framework Library namespace and classes. Then, you will see how to read and write XML documents. In the end of this article, I will show you how to take advantage of ADO.NET and XML .NET model to read and write XML documents from relational databases and vice versa.
Introduction to Microsoft .NET XML Namespaces and ClassesBefore start working with XML document in .NET Framework, It is important to know about .NET namespace and classes provided by .NET Runtime Library. .NET provides five namespace - System.Xml, System.Xml.Schema, System.Xml.Serialization, System.Xml.XPath, and System.Xml.Xsl to support XML classes. 
The System.Xml namespace contains major XML classes. This namespace contains many classes to read and write XML documents. In this article, we are going to concentrate on reader and write class. These reader and writer classes are used to read and write XMl documents. These classes are - XmlReader, XmlTextReader, XmlValidatingReader, XmlNodeReader, XmlWriter, and XmlTextWriter. As you can see there are four reader and two writer classes. 
The XmlReader class is an abstract bases classes and contains methods and properties to read a document. The Read method reads a node in the stream. Besides reading functionality, this class also contains methods to navigate through a document nodes. Some of these methods are MoveToAttribute, MoveToFirstAttribute, MoveToContent, MoveToFirstContent, MoveToElement and  MoveToNextAttribute. ReadString, ReadInnerXml, ReadOuterXml, and ReadStartElement are more read methods. This class also has a method Skip to skip current node and move to next one. We'll see these methods in our sample example. 
The XmlTextReader, XmlNodeReader and XmlValidatingReader classes are derived from XmlReader class. As their name explains, they are used to read text, node, and schemas.
The XmlWrite class contains functionality to write data to XML documents. This class provides many write method to write XML document items. This class is base class for XmlTextWriter class, which we'll be using in our sample example. 
The XmlNode class plays an important role. Although, this class represents a single node of XML but that could be the root node of an XML document and could represent the entire file. This class is an abstract base class for many useful classes for inserting, removing, and replacing nodes, navigating through the document. It also contains properties to get a parent or child, name, last child, node type and more. Three major classes derived from XmlNode are XmlDocument, XmlDataDocument and XmlDocumentFragment. XmlDocument class represents an XML document and provides methods and properties to load and save a document. It also provides functionality to add XML items such as attributes, comments, spaces, elements, and new nodes. The Load and LoadXml methods can be used to load XML documents and Save method to save a document respectively. XmlDocumentFragment class represents a document fragment, which can be used to add to a document. The XmlDataDocument class provides methods and properties to work with ADO.NET data set objects.
In spite of above discussed classes, System.Xml namespace contains more classes. Few of them are XmlConvert, XmlLinkedNode, and XmlNodeList. 
Next namespace in Xml series is System.Xml.Schema. It classes  to work with XML schemas such XmlSchema, XmlSchemaAll, XmlSchemaXPath, XmlSchemaType. 
The System.Xml.Serialization namespace contains classes that are used to serialize objects into XML format documents or streams. 
The System.Xml.XPath Namespce contains XPath related classes to use XPath specifications. This namespace has following classes  -XPathDocument, XPathExression, XPathNavigator, and XPathNodeIterator. With the help of XpathDocument, XpathNavigator provides a fast navigation though XML documents. This class contains many Move methods to move through a document. 
The System.Xml.Xsl namespace contains classes to work with XSL/T transformations.

Reading XML Documents

In my sample application, I'm using books.xml to read and display its data through XmlTextReader. This file comes with VS.NET samples. You can search this on your machine and change the path of the file in the following line: 
XmlTextReader textReader = new XmlTextReader("C:\\books.xml");Or you can use any XML file. 
The XmlTextReader, XmlNodeReader and XmlValidatingReader classes are derived from XmlReader class. Besides XmlReader methods and properties, these classes also contain members to read text, node, and schemas respectively. I am using XmlTextReader class to read an XML file. You read a file by passing file name as a parameter in constructor. 
XmlTextReader textReader = new XmlTextReader("C:\\books.xml");After creating an instance of XmlTextReader, you call Read method to start reading the document. After read method is called, you can read all information and data stored in a document. XmlReader class has properties such as Name, BaseURI, Depth, LineNumber an so on.
List 1 reads a document and displays a node information using these properties. 
About Sample Example 1In this sample example, I read an XML file using XmlTextReader and call Read method to read its node one by one until end of file and display the contents to the console output. 
Sample Example 1.

using System;
using System.Xml;
namespace ReadXml1
{
    class Class1
    {
        static void Main(string[] args)
        {
            // Create an isntance of XmlTextReader and call Read method to read the file
            XmlTextReader textReader = new XmlTextReader("C:\\books.xml");
            textReader.Read();
            // If the node has value
            while (textReader.Read())
            {
                // Move to fist element
                textReader.MoveToElement();
                Console.WriteLine("XmlTextReader Properties Test");
                Console.WriteLine("===================");
                // Read this element's properties and display them on console
                Console.WriteLine("Name:" + textReader.Name);
                Console.WriteLine("Base URI:" + textReader.BaseURI);
                Console.WriteLine("Local Name:" + textReader.LocalName);
                Console.WriteLine("Attribute Count:" + textReader.AttributeCount.ToString());
                Console.WriteLine("Depth:" + textReader.Depth.ToString());
                Console.WriteLine("Line Number:" + textReader.LineNumber.ToString());
                Console.WriteLine("Node Type:" + textReader.NodeType.ToString());
                Console.WriteLine("Attribute Count:" + textReader.Value.ToString());
            }
        }
    }
}


The NodeType property of XmlTextReader is important when you want to know the content type of a document. The XmlNodeType enumeration has a member for each type of XML item such as Attribute, CDATA, Element, Comment, Document, DocumentType, Entity, ProcessInstruction, WhiteSpace and so on.
List 2 code sample reads an XML document, finds a node type and writes information at the end with how many node types a document has. 

About Sample Example 2

In this sample example, I read an XML file using XmlTextReader and call Read method to read its node one by one until end of the file. After reading a node, I check its NodeType property to find the node and write node contents to the console and keep track of number of particular type of nodes. In the end, I display total number of different types of nodes in the document.
Sample Example 2.

using System;
using System.Xml;
namespace ReadingXML2
{
    class Class1
    {
        static void Main(string[] args)
        {
            int ws = 0;
            int pi = 0;
            int dc = 0;
            int cc = 0;
            int ac = 0;
            int et = 0;
            int el = 0;
            int xd = 0;
            // Read a document
            XmlTextReader textReader = new XmlTextReader("C:\\books.xml");
            // Read until end of file
            while (textReader.Read())
            {
                XmlNodeType nType = textReader.NodeType;
                // If node type us a declaration
                if (nType == XmlNodeType.XmlDeclaration)
                {
                    Console.WriteLine("Declaration:" + textReader.Name.ToString());
                    xd = xd + 1;
                }
                // if node type is a comment
                if (nType == XmlNodeType.Comment)
                {
                    Console.WriteLine("Comment:" + textReader.Name.ToString());
                    cc = cc + 1;
                }
                // if node type us an attribute
                if (nType == XmlNodeType.Attribute)
                {
                    Console.WriteLine("Attribute:" + textReader.Name.ToString());
                    ac = ac + 1;
                }
                // if node type is an element
                if (nType == XmlNodeType.Element)
                {
                    Console.WriteLine("Element:" + textReader.Name.ToString());
                    el = el + 1;
                }
                // if node type is an entity\
                if (nType == XmlNodeType.Entity)
                {
                    Console.WriteLine("Entity:" + textReader.Name.ToString());
                    et = et + 1;
                }
                // if node type is a Process Instruction
                if (nType == XmlNodeType.Entity)
                {
                    Console.WriteLine("Entity:" + textReader.Name.ToString());
                    pi = pi + 1;
                }
                // if node type a document
                if (nType == XmlNodeType.DocumentType)
                {
                    Console.WriteLine("Document:" + textReader.Name.ToString());
                    dc = dc + 1;
                }
                // if node type is white space
                if (nType == XmlNodeType.Whitespace)
                {
                    Console.WriteLine("WhiteSpace:" + textReader.Name.ToString());
                    ws = ws + 1;
                }
            }
            // Write the summary
            Console.WriteLine("Total Comments:" + cc.ToString());
            Console.WriteLine("Total Attributes:" + ac.ToString());
            Console.WriteLine("Total Elements:" + el.ToString());
            Console.WriteLine("Total Entity:" + et.ToString());
            Console.WriteLine("Total Process Instructions:" + pi.ToString());
            Console.WriteLine("Total Declaration:" + xd.ToString());
            Console.WriteLine("Total DocumentType:" + dc.ToString());
            Console.WriteLine("Total WhiteSpaces:" + ws.ToString());
        }
    }

}

Writing XML Documents

XmlWriter class contains the functionality to write to XML documents. It is an abstract base class used through XmlTextWriter and XmlNodeWriter classes. It contains methods and properties to write to XML documents. This class has several Writexxx method to write every type of item of an XML document. For example, WriteNode, WriteString, WriteAttributes, WriteStartElement, and WriteEndElement are some of them. Some of these methods are used in a start and end pair. For example, to write an element, you need to call WriteStartElement then write a string followed by WriteEndElement.              
Besides many methods, this class has three properties. WriteState, XmlLang, and XmlSpace. The WriteState gets and sets the state of the XmlWriter class
Although, it's not possible to describe all the Writexxx methods here, let's see some of them.
First thing we need to do is create an instance of XmlTextWriter using its constructor. XmlTextWriter has three overloaded constructors, which can take a string, stream, or a TextWriter as an argument. We'll pass a string (file name) as an argument, which we're going to create in C:\ root.
In my sample example, I create a file myXmlFile.xml in C:\\ root directory. 
// Create a new file in C:\\ dirXmlTextWriter textWriter = new XmlTextWriter("C:\\myXmFile.xml", null) ;After creating an instance, first thing you call us WriterStartDocument. When you're done writing, you call WriteEndDocument and TextWriter's Close method.

textWriter.WriteStartDocument();
textWriter.WriteEndDocument();
textWriter.Close();  
The WriteStartDocument and WriteEndDocument methods open and close a document for writing. You must have to open a document before start writing to it.  WriteComment method writes comment to a document. It takes only one string type of argument. WriteString method writes a string to a document. With the help of WriteString, WriteStartElement and WriteEndElement methods pair can be used to write an element to a document. The WriteStartAttribute and WriteEndAttribute pair writes an attribute.
WriteNode is more write method, which writes an XmlReader to a document as a node of the document. For example, you can use WriteProcessingInstruction and WriteDocType methods to write a ProcessingInstruction and DocType items of a document. 
//Write the ProcessingInstruction node string PI= "type='text/xsl' href='book.xsl'"
textWriter.WriteProcessingInstruction("xml-stylesheet", PI); 
//'Write the DocumentType node textWriter.WriteDocType("book", Nothing, Nothing, "<!ENTITY h 'softcover'>");The below sample example summarizes all these methods and creates a new xml document with some items in it such as elements, attributes, strings, comments and so on. See Listing 5-14. In this sample example, we create a new xml file c:\xmlWriterText.xml. In this sample example, We create a new xml file c:\xmlWriterTest.xml using XmlTextWriter: 
After that, we add comments and elements to the document using Writexxx methods. After that we read our books.xml xml file using XmlTextReader and add its elements to xmlWriterTest.xml using XmlTextWriter.

About Sample Example 3 

In this sample example, I create a new file myxmlFile.xml using XmlTextWriter and use its various write methods to write XML items. 
Sample Example 3.

using System;
using System.Xml;
namespace ReadingXML2
{
    class Class1
    {
        static void Main(string[] args)
        {
            // Create a new file in C:\\ dir
            XmlTextWriter textWriter = new XmlTextWriter("C:\\myXmFile.xml"null);
            // Opens the document
            textWriter.WriteStartDocument();
            // Write comments
            textWriter.WriteComment("First Comment XmlTextWriter Sample Example");
            textWriter.WriteComment("myXmlFile.xml in root dir");
            // Write first element
            textWriter.WriteStartElement("Student");
            textWriter.WriteStartElement("r""RECORD""urn:record");
            // Write next element
            textWriter.WriteStartElement("Name""");
            textWriter.WriteString("Student");
            textWriter.WriteEndElement();
            // Write one more element
            textWriter.WriteStartElement("Address"""); textWriter.WriteString("Colony");
            textWriter.WriteEndElement();
            // WriteChars
            char[] ch = new char[3];
            ch[0] = 'a';
            ch[1] = 'r';
            ch[2] = 'c';
            textWriter.WriteStartElement("Char");
            textWriter.WriteChars(ch, 0, ch.Length);
            textWriter.WriteEndElement();
            // Ends the document.
            textWriter.WriteEndDocument();
            // close writer
            textWriter.Close();
        }
    }
}



Using XmlDocument

The XmlDocument class represents an XML document. This class provides similar methods and properties we've discussed earlier in this article. 
Load and LoadXml are two useful methods of this class. A Load method loads XML data from a string, stream, TextReader or XmlReader. LoadXml method loads XML document from a specified string. Another useful method of this class is Save. Using Save method you can write XML data to a string, stream, TextWriter or XmlWriter.
About Sample Example 4This tiny sample example pretty easy to understand. We call LoadXml method of XmlDocument to load an XML fragment and call Save to save the fragment as an XML file. 
Sample Example 4.//Create the XmlDocument.XmlDocument doc = new XmlDocument();
doc.LoadXml(("<Student type='regular' Section='B'><Name>Tommy
ex</Name></Student>")); 
//Save the document to a file.doc.Save("C:\\std.xml");
You can also use Save method to display contents on console 
if you pass Console.Out as
 a
arameter. For example:
doc.Save(Console.Out);


About Sample Example 5 

Here is one example of how to load an XML document using XmlTextReader. In this sample example, we read books.xml file using XmlTextReader and call its Read method. After that we call XmlDocumetn's Load method to load XmlTextReader contents to XmlDocument and call Save method to save the document. Passing Console.Out as a Save method argument displays data on the console
Sample Example 5.XmlDocument doc = new XmlDocument();//Load the the document with the last book node.XmlTextReader reader = new XmlTextReader("c:\\books.xml");
reader.Read(); 
// load reader doc.Load(reader);// Display contents on the consoledoc.Save(Console.Out); 

Writing Data from a database to an XML Document


Using XML and ADO.NET mode, reading a database and writing to an XML document and vice versa is not a big deal. In this section of this article, you will see how to read a database table's data and write the contents to an XML document. 

The DataSet class provides method to read a relational database table and write this table to an XML file. You use WriteXml method to write a dataset data to an XML file.  
In this sample example, I have used commonly used Northwind database comes with Office 2000 and later versions. You can use any database you want. Only thing you need to do is just chapter the connection string and SELECT SQ L query. 

About Sample Example 6 

 In this sample, I reate a data adapter object and selects all records of Customers table. After that I can fill method to fill a dataset from the data adapter. 
In this sample example, I have used OldDb data provides. You need to add reference to the Syste.Data.OldDb namespace to use OldDb data adapters in your program. As you can see from Sample Example 6, first I create a connection with northwind database using OldDbConnection. After that I create a data adapter object by passing a SELECT SQL query and connection. Once you have a data adapter, you can fill a dataset object using Fill method of the data adapter. Then you can WriteXml method of DataSet, which creates an XML document and write its contents to the XML document. In our sample, we read Customers table records and write DataSet contents to OutputXml.Xml file in C:\ dir. 
Sample Example 6. 

using System;
using System.Xml;
using System.Data;
using System.Data.OleDb;
namespace ReadingXML2
{
    class Class1
    {
        static void Main(string[] args)
        {
            // create a connection
            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Northwind.mdb";
            // create a data adapter
            OleDbDataAdapter da = new OleDbDataAdapter("Select * from Customers", con);
            // create a new dataset
            DataSet ds = new DataSet();
            // fill dataset
            da.Fill(ds, "Customers");
            // write dataset contents to an xml file by calling WriteXml method
            ds.WriteXml("C:\\OutputXML.xml");
        }
    }
}