Home

XML Reading and Writing

 

Fundamentals of Writing to an XML File

 

Introduction

In the previous lessons, we learned to process XML files using the Document Object Model as implemented by the XmlDocument class. To go further and make XML friendlier, the .NET Framework provides many other classes for different purposes, allowing you to create and manage nodes from custom .NET classes.

Practical Learning Practical Learning: Introducing XML-Based Applications

  1. Start Microsoft Visual Studio .NET and create a Windows Forms Application named CPAS1
  2. Design the form as follows:
     
    Control Name Text Other Properties
    GroupBox   Customer and Car Information  
    Label   Customer Name:  
    TextBox txtCustomerName    
    Label   Address  
    TextBox txtAddress    
    Label   City:  
    TextBox txtCity    
    Label   State:  
    TextBox txtState    
    Label   ZIP Code:  
    TextBox txtZIPCode   TextAlign: Right
    Label   Make / Model:  
    TextBox txtMake    
    TextBox txtModel    
    Label   Year:  
    TextBox txtCarYear   TextAlign: Right
    Label   Problem Description:  
    TextBox txtProblem    
    GroupBox   Parts Used  
    Label   Part Name  
    Label   Unit Price  
    Label   Qty  
    Label   Sub Total  
    TextBox txtPartName1    
    TextBox txtUnitPrice1 0.00 TextAlign: Right
    TextBox txtQuantity1 0 TextAlign: Right
    TextBox txtSubTotal1 0.00 TextAlign: Right
    TextBox txtPartName2    
    TextBox txtUnitPrice2 0.00 TextAlign: Right
    TextBox txtQuantity2 0 TextAlign: Right
    TextBox txtSubTotal2 0.00 TextAlign: Right
    TextBox txtPartName3    
    TextBox txtUnitPrice3 0.00 TextAlign: Right
    TextBox txtQuantity3 0 TextAlign: Right
    TextBox txtSubTotal3 0.00 TextAlign: Right
    TextBox txtPartName4    
    TextBox txtUnitPrice4 0.00 TextAlign: Right
    TextBox txtQuantity4 0 TextAlign: Right
    TextBox txtSubTotal4 0.00 TextAlign: Right
    TextBox txtPartName5    
    TextBox txtUnitPrice5 0.00 TextAlign: Right
    TextBox txtQuantity5 0 TextAlign: Right
    TextBox txtSubTotal5 0.00 TextAlign: Right
    GroupBox   Jobs Performed  
    Label   Job Description  
    Label   Job Price  
    TextBox txtJobPerformed1    
    TextBox txtJobPrice1 0.00 TextAlign: Right
    TextBox txtJobPerformed2    
    TextBox txtJobPrice2 0.00 TextAlign: Right
    TextBox txtJobPerformed3    
    TextBox txtJobPrice3 0.00 TextAlign: Right
    TextBox txtJobPerformed4    
    TextBox txtJobPrice4 0.00 TextAlign: Right
    TextBox txtJobPerformed5    
    TextBox txtJobPrice5 0.00 TextAlign: Right
    GroupBox   Order Summary  
    Button btnCalculate Calculate Order  
    Label   Total Parts:  
    TextBox txtTotalParts 0.00 TextAlign: Right
    Label   Total Labor:  
    Text txtTotalLabor 0.00 TextAlign: Right
    Label   Tax Rate:  
    TextBox txtTaxRate 7.75 TextAlign: Right
    Label   %  
    Label   Tax Amount:  
    TextBox txtTaxAmount 0.00 TextAlign: Right
    Label   Total Order:  
    TextBox txtTotalOrder 0.00 TextAlign: Right
    Label   Recommendations  
    TextBox txtRecommendations   Multiline: True
    ScrollBars: Vertical
    Button btnSave Save this Order  
    Button btnOpen Open an Existing Order  
    Button btnReset Reset Order/Start New  
    Button btnClose Close  
  3. Double-click the Reset Order button and implement its Click event as follows:
     
    private void BtnResetOrderClick(object sender, System.EventArgs e)
    {
    	this.txtCustomerName.Text = "";
    	this.txtAddress.Text = "";
    	this.txtCity.Text = "";
    	this.txtState.Text = "";
    	this.txtZIPCode.Text = "";
    	this.txtMake.Text = "";
    	this.txtModel.Text = "";
    	this.txtCarYear.Text = "";
    	this.txtProblem.Text = "";
    
    	this.txtPartName1.Text = "";
    	this.txtUnitPrice1.Text = "0.00";
    	this.txtQuantity1.Text  = "0";
    	this.txtSubTotal1.Text  = "0.00";
    	this.txtPartName2.Text = "";
    	this.txtUnitPrice2.Text = "0.00";
    	this.txtQuantity2.Text  = "0";
    	this.txtSubTotal2.Text  = "0.00";
    	this.txtPartName3.Text = "";
    	this.txtUnitPrice3.Text = "0.00";
    	this.txtQuantity3.Text  = "0";
    	this.txtSubTotal3.Text  = "0.00";
    	this.txtPartName4.Text = "";
    	this.txtUnitPrice4.Text = "0.00";
    	this.txtQuantity4.Text  = "0";
    	this.txtSubTotal4.Text  = "0.00";
    	this.txtPartName5.Text = "";
    	this.txtUnitPrice5.Text = "0.00";
    	this.txtQuantity5.Text  = "0";
    	this.txtSubTotal5.Text  = "0.00";
    
    	this.txtJobPerformed1.Text = "";
    	this.txtJobPrice1.Text = "0.00";
    	this.txtJobPerformed2.Text = "";
    	this.txtJobPrice2.Text = "0.00";
    	this.txtJobPerformed3.Text = "";
    	this.txtJobPrice3.Text = "0.00";
    	this.txtJobPerformed4.Text = "";
    	this.txtJobPrice4.Text = "0.00";
    	this.txtJobPerformed5.Text = "";
    	this.txtJobPrice5.Text = "0.00";
    
    	this.txtTotalParts.Text = "0.00";
    	this.txtTotalLabor.Text = "0.00";
    	this.txtTaxRate.Text    = "7.75";
    	this.txtTaxAmount.Text  = "0.00";
    	this.txtTotalOrder.Text = "0.00";
    
    	this.txtRecommendations.Text = "";
    	this.txtCustomerName.Focus();
    }
  4. Double-click the Calculate Order button and implement its Click event as follows:
      
    private void BtnCalculateOrderClick(object sender, System.EventArgs e)
    {
    	decimal part1UnitPrice = 0.00M,
    		    part1SubTotal  = 0.00M,
    		    part2UnitPrice = 0.00M,
    		    part2SubTotal  = 0.00M,
    		    part3UnitPrice = 0.00M,
    		    part3SubTotal  = 0.00M,
    		    part4UnitPrice = 0.00M,
    		    part4SubTotal  = 0.00M,
    		    part5UnitPrice = 0.00M,
    		    part5SubTotal  = 0.00M,
    		    totalParts     = 0.00M;
    	int    part1Quantity = 0, part2Quantity = 0, part3Quantity = 0,
    		   part4Quantity = 0, part5Quantity = 0;
    	decimal job1Price = 0.00M,
    		    job2Price = 0.00M,
    		    job3Price = 0.00M,
    		    job4Price = 0.00M,
    		    job5Price = 0.00M;
    	decimal totalLabor = 0.00M;
    	decimal taxRate = 0.00M, taxAmount = 0.00M, totalOrder = 0.00M;
    
    	// Don't charge a part unless it is clearly identified
    	if( this.txtPartName1.Text == "" )
    	{
    		this.txtUnitPrice1.Text = "0.00";
    		this.txtQuantity1.Text  = "0";
    		this.txtSubTotal1.Text  = "0.00";
    		part1UnitPrice = 0.00M;
    	}
    	else
    	{
    		try 
    		{
    			part1UnitPrice = decimal.Parse(this.txtUnitPrice1.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Unit Price");
    			this.txtUnitPrice1.Text = "0.00";
    			this.txtUnitPrice1.Focus();
    		}
    
    		try 
    		{
    			part1Quantity = int.Parse(this.txtQuantity1.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Quantity");
    			this.txtQuantity1.Text = "0";
    			this.txtQuantity1.Focus();
    		}
    	}
    
    	if( this.txtPartName2.Text == "" )
    	{
    		this.txtUnitPrice2.Text = "0.00";
    		this.txtQuantity2.Text  = "0";
    		this.txtSubTotal2.Text  = "0.00";
    		part2UnitPrice = 0.00M;
    	}
    	else
    	{
    		try 
    		{
    			part2UnitPrice = decimal.Parse(this.txtUnitPrice2.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Unit Price");
    			this.txtUnitPrice2.Text = "0.00";
    			this.txtUnitPrice2.Focus();
    		}
    
    		try 
    		{
    			part2Quantity = int.Parse(this.txtQuantity2.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Quantity");
    			this.txtQuantity2.Text = "0";
    			this.txtQuantity2.Focus();
    		}
    	}
    
    	if( this.txtPartName3.Text == "" )
    	{
    		this.txtUnitPrice3.Text = "0.00";
    		this.txtQuantity3.Text  = "0";
    		this.txtSubTotal3.Text  = "0.00";
    		part3UnitPrice = 0.00M;
    	}
    	else
    	{
    		try 
    		{
    			part3UnitPrice = decimal.Parse(this.txtUnitPrice3.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Unit Price");
    			this.txtUnitPrice3.Text = "0.00";
    			this.txtUnitPrice3.Focus();
    		}
    			try 
    		{
    			part3Quantity = int.Parse(this.txtQuantity3.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Quantity");
    			this.txtQuantity3.Text = "0";
    			this.txtQuantity3.Focus();
    		}
    	}
    
    	if( this.txtPartName4.Text == "" )
    	{
    		this.txtUnitPrice4.Text = "0.00";
    		this.txtQuantity4.Text  = "0";
    		this.txtSubTotal4.Text  = "0.00";
    		part4UnitPrice = 0.00M;
    	}
    	else
    	{
    		try 
    		{
    			part4UnitPrice = decimal.Parse(this.txtUnitPrice4.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Unit Price");
    			this.txtUnitPrice4.Text = "0.00";
    			this.txtUnitPrice4.Focus();
    		}
    
    		try 
    		{
    			part4Quantity = int.Parse(this.txtQuantity4.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Quantity");
    					this.txtQuantity4.Text = "0";
    					this.txtQuantity4.Focus();
    		}
    	}
    
    	if( this.txtPartName5.Text == "" )
    	{
    		this.txtUnitPrice5.Text = "0.00";
    		this.txtQuantity5.Text  = "0";
    		this.txtSubTotal5.Text  = "0.00";
    		part5UnitPrice = 0.00M;
    	}
    	else
    	{
    		try 
    		{
    			part5UnitPrice = decimal.Parse(this.txtUnitPrice5.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Unit Price");
    			this.txtUnitPrice5.Text = "0.00";
    			this.txtUnitPrice5.Focus();
    		}
    
    		try 
    		{
    			part5Quantity = int.Parse(this.txtQuantity5.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Quantity");
    			this.txtQuantity5.Text = "0";
    			this.txtQuantity5.Focus();
    		}
    	}
    
    	// Don't bill the customer for a job that is not specified
    	if( this.txtJobPerformed1.Text == "" )
    	{
    		this.txtJobPrice1.Text = "0.00";
    		job1Price = 0.00M;
    	}
    	else
    	{
    		try 
    		{
    			job1Price = decimal.Parse(this.txtJobPrice1.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Job Price");
    			this.txtJobPrice1.Text = "0.00";
    			this.txtJobPrice1.Focus();
    		}
    	}
    				 
    	if( this.txtJobPerformed2.Text == "" )
    	{
    		this.txtJobPrice2.Text = "0.00";
    		job2Price = 0.00M;
    	}
    	else
    	{
    		try 
    		{
    			job2Price = decimal.Parse(this.txtJobPrice2.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Job Price");
    			this.txtJobPrice2.Text = "0.00";
    			this.txtJobPrice2.Focus();
    		}
    	}
    				 
    	if( this.txtJobPerformed3.Text == "" )
    	{
    		this.txtJobPrice3.Text = "0.00";
    		job3Price = 0.00M;
    	}
    	else
    	{
    		try 
    		{
    			job3Price = decimal.Parse(this.txtJobPrice3.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Job Price");
    			this.txtJobPrice3.Text = "0.00";
    			this.txtJobPrice3.Focus();
    		}
    	}
    			 
    	if( this.txtJobPerformed4.Text == "" )
    	{
    		this.txtJobPrice4.Text = "0.00";
    		job4Price = 0.00M;
    	}
    	else
    	{
    		try 
    		{
    			job4Price = decimal.Parse(this.txtJobPrice4.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Job Price");
    			this.txtJobPrice4.Text = "0.00";
    			this.txtJobPrice4.Focus();
    		}
    	}
    				 
    	if( this.txtJobPerformed5.Text == "" )
    	{
    		this.txtJobPrice5.Text = "0.00";
    		job5Price = 0.00M;
    	}
    	else
    	{
    		try 
    		{
    			job5Price = decimal.Parse(this.txtJobPrice5.Text);
    		}
    		catch(FormatException )
    		{
    			MessageBox.Show("Invalid Job Price");
    			this.txtJobPrice5.Text = "0.00";
    			this.txtJobPrice5.Focus();
    		}
    	}
    		 
    	part1SubTotal = part1UnitPrice * part1Quantity;
    	part2SubTotal = part2UnitPrice * part2Quantity;
    	part3SubTotal = part3UnitPrice * part3Quantity;
    	part4SubTotal = part4UnitPrice * part4Quantity;
    	part5SubTotal = part5UnitPrice * part5Quantity;
    
    	this.txtSubTotal1.Text = part1SubTotal.ToString("F");
    	this.txtSubTotal2.Text = part2SubTotal.ToString("F");
    	this.txtSubTotal3.Text = part3SubTotal.ToString("F");
    	this.txtSubTotal4.Text = part4SubTotal.ToString("F");
    	this.txtSubTotal5.Text = part5SubTotal.ToString("F");
    
    	totalParts    = part1SubTotal + part2SubTotal + part3SubTotal +
    		part4SubTotal + part5SubTotal;
    		 
    	totalLabor    = job1Price + job2Price + job3Price +
    		job4Price + job5Price;
    				 
    	try 
    	{
    		taxRate = decimal.Parse(this.txtTaxRate.Text);
    	}
    	catch(FormatException )
    	{
    		MessageBox.Show("Invalid Tax Rate");
    		this.txtTaxRate.Text = "7.75";
    		this.txtTaxRate.Focus();
    	}
    				 
    	decimal totalPartsAndLabor = totalParts + totalLabor;
    	taxAmount  = totalPartsAndLabor * taxRate / 100;
    	totalOrder = totalPartsAndLabor + taxAmount;
    				 
    	this.txtTotalParts.Text = totalParts.ToString("F");
    	this.txtTotalLabor.Text = totalLabor.ToString("F");
    	this.txtTaxAmount.Text  = taxAmount.ToString("F");
    	this.txtTotalOrder.Text = totalOrder.ToString("F");
    }
  5. Return to the Design view of the form and click the first text box under Qty
  6. In the Properties window, click the Events button and double-click Leave
  7. Implement it as follows:
     
    void TxtQuantity1Leave(object sender, System.EventArgs e)
    {
    	this.BtnCalculateClick(sender, e);
    }		
  8. Return to the Design view of the form and click the second text box under Qty
  9. In the Events section of the Properties window, double-click Leave and implement it as follows:
     
    void TxtQuantity2Leave(object sender, System.EventArgs e)
    {
    	this.BtnCalculateClick(sender, e);
    }		
  10. Return to the Design view of the form and click the third text box under Qty
  11. In the Events section of the Properties window, double-click Leave and implement it as follows:
     
    void TxtQuantity3Leave(object sender, System.EventArgs e)
    {
    	this.BtnCalculateClick(sender, e);
    }		
  12. Return to the Design view of the form and click the fourth text box under Qty
  13. In the Events section of the Properties window, double-click Leave and implement it as follows:
     
    void TxtQuantity4Leave(object sender, System.EventArgs e)
    {
    	this.BtnCalculateClick(sender, e);
    }		
  14. Return to the Design view of the form and click the fifth text box under Qty
  15. In the Events section of the Properties window, double-click Leave and implement it as follows:
     
    void TxtQuantity5Leave(object sender, System.EventArgs e)
    {
    	this.BtnCalculateClick(sender, e);
    }		
  16. Return to the Design view of the form and click the first text box under Job Price
  17. In the Events section of the Properties window, double-click Leave and implement it as follows:
     
    void TxtJobPrice1Leave(object sender, System.EventArgs e)
    {
    	this.BtnCalculateClick(sender, e);
    }		
  18. Return to the Design view of the form and click the second text box under Job Price
  19. In the Events section of the Properties window, double-click Leave and implement it as follows:
     
    void TxtJobPrice2Leave(object sender, System.EventArgs e)
    {
    	this.BtnCalculateClick(sender, e);
    }		
  20. Return to the Design view of the form and click the third text box under Job Price
  21. In the Events section of the Properties window, double-click Leave and implement it as follows:
     
    void TxtJobPrice3Leave(object sender, System.EventArgs e)
    {
    	this.BtnCalculateClick(sender, e);
    }		
  22. Return to the Design view of the form and click the fourth text box under Job Price
  23. In the Events section of the Properties window, double-click Leave and implement it as follows:
     
    void TxtJobPrice4Leave(object sender, System.EventArgs e)
    {
    	this.BtnCalculateClick(sender, e);
    }		
  24. Return to the Design view of the form and click the fifth text box under Job Price
  25. In the Events section of the Properties window, double-click Leave and implement it as follows:
     
    void TxtJobPrice5Leave(object sender, System.EventArgs e)
    {
    	this.BtnCalculateClick(sender, e);
    }
  26. Execute the application to test it and fill the form with a record
  27. Click the Calculate Order button
  28. Close the form and return to your programming environment

The XML Text Writer Fundamentals

Besides the XmlDocument and the derived classes of XmlNode, the .NET Framework provides the XmlTextWriter class, which is derived from XmlWriter. The XmlTextWriter class works in a top-down approach to create, or deal with, the contents of an XML file. This class writes an XML node and moves down without referring back:

This means that, once you have created a node using the XmlTextWriter, you have no way of referring back to it.

To use an XmlTextWriter object, first declare a pointer to this class and initialize the variable using one of its three constructors.

If you had already created a Stream-based object such as declaring a pointer to FileStream but you didn't defined an encoding scheme, you can pass the Stream-based object to an XmlTextWriter but you must take this time to specify the encoding scheme. To support this concept, the XmlTextWriter provides a constructor with the following syntax:

public XmlTextWriter(Stream w,  Encoding encoding);

The first argument to this constructor can be a Stream-derived variable. The second argument specifies the encoding scheme that would be applied. The default is UTF-8. Based on this, if you want to use the UTF-8 encoding scheme, you can pass it or pass the argument as 0. If you want to use another encoding scheme, pass it to the constructor.

To work from scratch, that is, to initiate a file with manually-created nodes, you can pass the desired name of the file to the XmlTextWriter constructor using the following syntax:

public XmlTextWriter(string filename, Encoding encoding);

In this case, you must provide the name  or path to the file, whose content you are creating, as the first argument. You must pass the desired encoding scheme as the second argument. Here is an example:

void BtnSaveClick(System.Object  sender, System.EventArgs  e)
{
	 XmlTextWriter xmlTxtWriter = new XmlTextWriter("Videos.xml", Encoding.UTF8);
}

Eventually, when you have finished using the XmlTextWriter object, you must free the memory it was using by calling the XmlTextWriter.Flush() method. To release the resources that the object was using, call the XmlTextWriter.Close() method. Here is an example:

void BtnSaveClick(System.Object sender, System.EventArgs  e)
{
	 XmlTextWriter xmlTxtWriter = new XmlTextWriter("Videos.xml", Encoding.UTF8);

	 xmlTxtWriter.Flush();
	 xmlTxtWriter.Close();
}

Creating the XML Declaration

Declaring an XmlTextWriter variable allows you to indicate that you intend to create a new XML file. With the variable ready, you can start writing the file's content. As mentioned in previous lessons, an XML file starts at the top with an XML declaration. To create this declaration, you can call the XmlTextWriter.WriteStartDocument() method. This method is overloaded with two versions. The syntax of one of them is:

public void WriteStartDocument();

This method creates a declaration, sets the XML version to 1.0, and includes the encoding scheme you specified in the constructor. Here is an example:

void BtnSaveClick(System.Object sender, System.EventArgs e)
{
	 XmlTextWriter xmlTxtWriter = new XmlTextWriter("Videos.xml", Encoding.UTF8);

	 xmlTxtWriter.WriteStartDocument();

	 xmlTxtWriter.Flush();
	 xmlTxtWriter.Close();
}

To end an XML file, you must close its declaration. This is done by calling the XmlTextWriter.WriteEndDocument() method. Its syntax is:

public void WriteEndDocument();

This method indicates that the XML file has ended and allows the compiler to stop reading it down. This would be done as follows:

void btnSaveClick(System.Object sender, System.EventArgs e)
{
	 XmlTextWriter xmlTxtWriter = new XmlTextWriter("Videos.xml", Encoding.UTF8);

	 xmlTxtWriter.WriteStartDocument();
				
	 xmlTxtWriter.WriteEndDocument();

	 xmlTxtWriter.Flush();
	 xmlTxtWriter.Close();
}

Creating the Root Element

Between the XML declaration and the end of the file, that is, between the call to XmlTextWriter.WriteStartDocument() and the call to XmlTextWriter.WriteEndDocument() methods, you can create the necessary nodes of the file. As reviewed in previous lessons, the most regular node of an XML file is the element. To create an element, the XmlTextWriter class provides the WriteStartElement() method that is overloaded with various versions. One of the versions of this method and that is inherited from the XmlWriter class uses the following syntax:

public void WriteStartElement(string localName);

This method takes as argument the name of the element that will be created. Here is an example:

void BtnSaveClick(System.object sender, System.EventArgs e)
{
	 XmlTextWriter xmlTxtWriter = new XmlTextWriter("Videos.xml", Encoding.UTF8);

	 xmlTxtWriter.WriteStartDocument();
				
	 xmlTxtWriter.WriteStartElement("Video");

	 xmlTxtWriter.WriteEndDocument();

	 xmlTxtWriter.Flush();
	 xmlTxtWriter.Close();
}

As you may know from XML, every element must be closed. To close an XML element, call the XmlTextWriter.WriteEndElement() method. Its syntax is:

public override void WriteEndElement();

When calling this method, always make sure that you know the element it is closing, which must corresponding to an appropriate previous call to a WriteStartElement() method. Here is an example:

void BtnSaveClick(System.object sender, System.EventArgs e)
{
	 XmlTextWriter xmlTxtWriter = new XmlTextWriter("Videos.xml", Encoding.UTF8);

	 xmlTxtWriter.WriteStartDocument();
				
	 xmlTxtWriter.WriteStartElement("Video");
	 xmlTxtWriter.WriteEndElement();

	 xmlTxtWriter.WriteEndDocument();

	 xmlTxtWriter.Flush();
	 xmlTxtWriter.Close();
}

This would produce:

As you can see from the result, a single or the first call to the WriteStartElement() method creates the root element that is required for every XML file. This means that, after this (first) call but before its corresponding WriteEndElement() call, you can create the necessary nodes that you want to include as part of the file.

Creating the Child Elements of the Root

To help you create child elements of the root node, you can keep calling the XmlTextWriter.WriteStartElement() method as necessary and appropriately closing it. Here is an example:

void BtnSaveClick(System.object sender, System.EventArgs e)
{
	 XmlTextWriter xmlTxtWriter = new XmlTextWriter("Videos.xml", Encoding.UTF8);

	 xmlTxtWriter.WriteStartDocument();
				
	 xmlTxtWriter.WriteStartElement("Video");
	 xmlTxtWriter.WriteStartElement("Video");

	 xmlTxtWriter.WriteEndElement();
	 xmlTxtWriter.WriteEndElement();

	 xmlTxtWriter.WriteEndDocument();

	 xmlTxtWriter.Flush();
	 xmlTxtWriter.Close();
}

If you simply call this method as done above, the element would be empty. Based on this, the above code would produce:

If you want the element to have a value, call the XmlTextWriter.WriteString() method. Its syntax is:

public void WriteString(string text);

This method must immediately follow the call to WriteStartElement() that creates a new element. It takes as argument the value for the immediately previously defined element. Here is an example:

void BtnSaveClick(System.object sender, System.EventArgs e)
{
	 XmlTextWriter xmlTxtWriter = new XmlTextWriter("Videos.xml", Encoding.UTF8);

	 xmlTxtWriter.WriteStartDocument();
				
	 xmlTxtWriter.WriteStartElement("Video");

	 xmlTxtWriter.WriteStartElement(Video");
	 xmlTxtWriter.WriteString("The Distinguished Gentleman");
	 xmlTxtWriter.WriteEndElement();
				 
	 xmlTxtWriter.WriteEndElement();

	 xmlTxtWriter.WriteEndDocument();

	 xmlTxtWriter.Flush();
	 xmlTxtWriter.Close();
}

 This would produce:

If you call the XmlTextWriter.WriteStartElement() method and you want the element to have a value, remember to call the XmlTextWriter.WriteString() method, and then call the XmlTextWriter.WriteEndElement() method. An alternative is to call the XmlWriter.WriteElementString() method that comes in two versions. The syntax of one of these versions is:

public void WriteElementString(string localName, string value);

The first argument to this method is the name of the element that will be created. The second argument is the value of the element. This method creates and closes its element. Here is an example:

void BtnSaveClick(System.object sender, System.EventArgs e)
{
	 XmlTextWriter xmlTxtWriter = new XmlTextWriter("Videos.xml", Encoding.UTF8);

	 xmlTxtWriter.WriteStartDocument();
				
	 xmlTxtWriter.WriteStartElement("Video");

	 xmlTxtWriter.WriteStartElement("Video");
	 xmlTxtWriter.WriteString("The Distinguished Gentleman");
	 xmlTxtWriter.WriteEndElement();
				 
	 xmlTxtWriter.WriteElementString("Video", "Fatal Attraction");

	 xmlTxtWriter.WriteEndElement();

	 xmlTxtWriter.WriteEndDocument();

	 xmlTxtWriter.Flush();
	 xmlTxtWriter.Close();
}

This would produce:

In the same way, you can create the necessary elements and their child elements as necessary. Be careful to appropriately start an element and remember to close it if necessary. Here are examples:

void BtnSaveClick(System.object sender, System.EventArgs e)
{
	 XmlTextWriter xmlTxtWriter = new XmlTextWriter("Videos.xml", Encoding.UTF8);

	 // The XML declaration
	 xmlTxtWriter.WriteStartDocument();
				
	 // The root element
	 xmlTxtWriter.WriteStartElement("Video");

	 // Start a Video element
	 xmlTxtWriter.WriteStartElement("Video");

	 // Create a Title child element to the Video element
	 xmlTxtWriter.WriteStartElement("Title");
	 xmlTxtWriter.WriteString("The Distinguished Gentleman");
	 xmlTxtWriter.WriteEndElement();

	 // Create a Director child element to the Video element
	 xmlTxtWriter.WriteStartElement("Director");
	 xmlTxtWriter.WriteString("Jonathan Lynn");
	 xmlTxtWriter.WriteEndElement();

	 // Create a Length child element to the Video element
	 xmlTxtWriter.WriteStartElement("Length");
	 xmlTxtWriter.WriteString("112 Minute");
	 xmlTxtWriter.WriteEndElement();

	 // Create a Format child element to the Video element
	 xmlTxtWriter.WriteStartElement("Format");
	 xmlTxtWriter.WriteString("DVD");
	 xmlTxtWriter.WriteEndElement();

	 // Create a Rating child element to the Video element
	 xmlTxtWriter.WriteStartElement("Rating");
	 xmlTxtWriter.WriteString("R");
	 xmlTxtWriter.WriteEndElement();

	 xmlTxtWriter.WriteEndElement();
				 
	 // Start a Video element
	 xmlTxtWriter.WriteStartElement("Video");
	 // Create the child elements of the second Video element
	 xmlTxtWriter.WriteElementString("Title", "Fatal Attraction");
	 xmlTxtWriter.WriteElementString("Director", "Adrian Lyne");
	 xmlTxtWriter.WriteElementString("Length", "120 Minute");
	 xmlTxtWriter.WriteElementString("Format", "VH");
	 xmlTxtWriter.WriteElementString("Rating", "R");

	 // Close a Video element
	 xmlTxtWriter.WriteEndElement();

	 // Close the root element
	 xmlTxtWriter.WriteEndDocument();

	 xmlTxtWriter.Flush();
	 xmlTxtWriter.Close();
}

This would produce:

 

Practical Learning Practical Learning: Creating the Elements of an XML File

  1. Click the Design tab of the form.
    In the Tools window, click SaveFileDialog and click the form
  2. While the saveFileDialog1 control is still selected, in the Properties window, change its characteristics as follows:
    DefaultExt: xml
    Filter: Repair Orders (*.xml)|*.xml|All files (*.*)|*.*"

    Title:  Save Current Repair Order
  3. Double-click the Save this Order button
  4. In the top section of the file, under the other using namespace lines, type the following:
     
    using System.IO;
    using System.Xml;
    using System.Text;
  5. To create complete the XML file with the desired elements, implement the event as follows:
     
    void BtnSaveClick(object sender, System.EventArgs e)
    {	
    	 string strFilename = null;
    	 string strCustomerStartName = null;
    	 string strCarInitials = null;
    
    	 if( this.txtCustomerName.Text != "" )
    		 strCustomerStartName = this.txtCustomerName.Text.Substring(0, 2);
    	 
    	 if( this.txtMake.Text != "" )
    		 strCarInitials = String.Concat(strCarInitials, this.txtMake.Text.Substring(0, 1));
    	 if( this.txtModel.Text != "" )
    		 strCarInitials = String.Concat(strCarInitials, this.txtModel.Text.Substring(0, 1));
    	 if( this.txtCarYear.Text != "" )
    		 strCarInitials = String.Concat(strCarInitials, this.txtCarYear.Text);
    
    	 strFilename = String.Concat(strFilename, strCarInitials, strCustomerStartName, ".xml");
    
    	 saveFileDialog1.FileName = strFilename;
    	 
    	 if( this.saveFileDialog1.ShowDialog() == DialogResult.OK )
    	 {
    		 XmlTextWriter wtrRepairOrder = new XmlTextWriter(strFilename, Encoding.UTF8);
    
    		 // Create the contents of the XML file
    		 // Notice that we are not making an attempt to check the values
    		 wtrRepairOrder.WriteStartDocument();
    
    		 wtrRepairOrder.WriteStartElement("CustomerOrder");
    		 wtrRepairOrder.WriteStartElement("RepairOrder");
    
    		 wtrRepairOrder.WriteElementString("CustomerName", this.txtCustomerName.Text);
    		 wtrRepairOrder.WriteElementString("Addres", this.txtAddress.Text);
    		 wtrRepairOrder.WriteElementString("City", this.txtCity.Text);
    		 wtrRepairOrder.WriteElementString("State", this.txtState.Text);
    		 wtrRepairOrder.WriteElementString("ZIPCode", this.txtZIPCode.Text);
    		 wtrRepairOrder.WriteElementString("Make", this.txtMake.Text);
    		 wtrRepairOrder.WriteElementString("Model", this.txtModel.Text);
    		 wtrRepairOrder.WriteElementString("CarYear", this.txtCarYear.Text);
    		 wtrRepairOrder.WriteElementString("ProbDescription", this.txtProblem.Text);
    		 wtrRepairOrder.WriteElementString("PartName1", this.txtPartName1.Text);
    		 wtrRepairOrder.WriteElementString("UnitPrice1", this.txtUnitPrice1.Text);
    		 wtrRepairOrder.WriteElementString("Quantity1", this.txtQuantity1.Text);
    		 wtrRepairOrder.WriteElementString("SubTotal1", this.txtSubTotal1.Text);
    		 wtrRepairOrder.WriteElementString("PartName2", this.txtPartName2.Text);
    		 wtrRepairOrder.WriteElementString("UnitPrice2", this.txtUnitPrice2.Text);
    		 wtrRepairOrder.WriteElementString("Quantity2", this.txtQuantity2.Text);
    		 wtrRepairOrder.WriteElementString("SubTotal2", this.txtSubTotal2.Text);
    		 wtrRepairOrder.WriteElementString("PartName3", this.txtPartName3.Text);
    		 wtrRepairOrder.WriteElementString("UnitPrice3", this.txtUnitPrice3.Text);
    		 wtrRepairOrder.WriteElementString("Quantity3", this.txtQuantity3.Text);
    		 wtrRepairOrder.WriteElementString("SubTotal3", this.txtSubTotal3.Text);
    		 wtrRepairOrder.WriteElementString("PartName4", this.txtPartName4.Text);
    		 wtrRepairOrder.WriteElementString("UnitPrice4", this.txtUnitPrice4.Text);
    		 wtrRepairOrder.WriteElementString("Quantity4", this.txtQuantity4.Text);
    		 wtrRepairOrder.WriteElementString("SubTotal4", this.txtSubTotal4.Text);
    		 wtrRepairOrder.WriteElementString("PartName5", this.txtPartName5.Text);
    		 wtrRepairOrder.WriteElementString("UnitPrice5", this.txtUnitPrice5.Text);
    		 wtrRepairOrder.WriteElementString("Quantity5", this.txtQuantity5.Text);
    		 wtrRepairOrder.WriteElementString("SubTotal5", this.txtSubTotal5.Text);
    		 wtrRepairOrder.WriteElementString("Job1", this.txtJobPerformed1.Text);
    		 wtrRepairOrder.WriteElementString("JobPrice1", this.txtJobPrice1.Text);
    		 wtrRepairOrder.WriteElementString("Job2", this.txtJobPerformed2.Text);
    		 wtrRepairOrder.WriteElementString("JobPrice2", this.txtJobPrice2.Text);
    		 wtrRepairOrder.WriteElementString("Job3", this.txtJobPerformed3.Text);
    		 wtrRepairOrder.WriteElementString("JobPrice3", this.txtJobPrice3.Text);
    		 wtrRepairOrder.WriteElementString("Job4", this.txtJobPerformed4.Text);
    		 wtrRepairOrder.WriteElementString("JobPrice4", this.txtJobPrice4.Text);
    		 wtrRepairOrder.WriteElementString("Job5", this.txtJobPerformed5.Text);
    		 wtrRepairOrder.WriteElementString("JobPrice5", this.txtJobPrice5.Text);
    		 wtrRepairOrder.WriteElementString("TotalPart", this.txtTotalParts.Text);
    		 wtrRepairOrder.WriteElementString("TotalLabor", this.txtTotalLabor.Text);
    		 wtrRepairOrder.WriteElementString("TaxRate", this.txtTaxRate.Text);
    		 wtrRepairOrder.WriteElementString("TaxAmount", this.txtTaxAmount.Text);
    		 wtrRepairOrder.WriteElementString("TotalOrder", this.txtTotalOrder.Text);
    		 wtrRepairOrder.WriteElementString("Recommendation", this.txtRecommendations.Text);
    
    		 wtrRepairOrder.WriteEndElement();
    		 wtrRepairOrder.WriteEndElement();
    
    		 wtrRepairOrder.WriteEndDocument();
    
    		 wtrRepairOrder.Flush();
    		 wtrRepairOrder.Close();
    	 }
    }
  6. Execute the application to test it
  7. Create a new record and click Calculate Order:
     
  8. Click the Save this Order and Start New Order button:
     
  9. After setting the name, click Save
  10. Create another order, calculate it and save it
  11. Close the form
  12. Use the browser to preview the newly saved record 
  13. Return to your programming environment

Saving a File As XML

If you are creating a text-intensive document and you want to save it as an XML file, for example if you have declared a variable of type TextWriter-derived class (such as StringWriter or StreamWriter), you can use that file to initialize the XmlTextWriter variable. To support TextWriter documents, the XmlTextWriter provides a constructor with the following syntax:

public XmlTextWriter(TextWriter w);

This constructor expects as argument TextWriter-based object. This means that you should have defined the TextWriter object prior to passing it to this constructor. This also implies that the TextWriter was used to specify the encoding scheme that would be used. Here is an example:

void BtnSaveClick(System.object sender, System.EventArgs e)
{
	 FileStream fleStream = new FileStream("Videos.xml", FileMode.Create, FileAccess.Write, FileShare.None); 
	 StreamWriter stmWriter = new StreamWriter(fleStream);
	 XmlTextWriter xmlTxtWriter = new XmlTextWriter(stmWriter);

				 
	 stmWriter.Flush();
	 stmWriter.Close();
}

To actually write the contents of the document, you can create each paragraph by calling the XmlTextWriter.WriteStartElement() method the same way we did earlier. Here is an example:

void BtnSaveClick(System.object sender, System.EventArgs e)
{
	 FileStream fleStream = new FileStream("Memorandum.xml", FileMode.Create, FileAccess.Write, FileShare.None); 
	 StreamWriter stmWriter = new StreamWriter(fleStream);
	 XmlTextWriter xmlTxtWriter = new XmlTextWriter(stmWriter);
	 xmlTxtWriter.Formatting = Formatting.Indented;

	 xmlTxtWriter.WriteStartDocument();
	 xmlTxtWriter.WriteStartElement("ToAllEmployee");

	 for(int i = 0; i < this.txtEditor.Lines.Length; i++)
	 {
		 xmlTxtWriter.WriteStartElement("Notice");
		 xmlTxtWriter.WriteString(this.txtEditor.Lines[i].ToString());
		 xmlTxtWriter.WriteEndElement();
	 }

	 xmlTxtWriter.WriteEndDocument();
	 stmWriter.Flush();
	 stmWriter.Close();
}

Reading From an XML File

 

Introduction

Once an XML file exists, you can read it to retrieve the values of its nodes. To support opening an XML file and reading its contents, the .NET Framework provides the XmlTextReader class that is derived from the XmlReader class. The XmlTextReader class is equipped with all the necessary properties and methods to explore the contents of an XML file.

Like XmlTextWriter, the XmlTextReader class reads a file from top to bottom without going back up once it has passed a node:

This means that, when using the XmlTextReader class to read an XML file, once you have read a node and moved down the file, you cannot refer back to the previous node and you cannot access a previous node: once you have passed a node, you cannot access it anymore.

Using and XML Text Reader

To use an XML text reader, declare a pointer to XmlTextReader and initialize it with one of its constructors. This class is equipped with 14 constructors. If you want to open a file whose name or path you know, use the following constructor:

public XmlTextReader(string url);

This constructor takes as argument the name or path to an XML file. If the file is found, then it would be opened. If the file doesn't exist or there is an error in the string that specifies its path, the compiler would throw an XmlException exception.

Reading Nodes

After declaring an XmlTextReader variable, you can start reading the content of the file. To support this, you can call the XmlTextReader.Read() method. Its syntax is:

public override bool Read();

As mentioned previously, the file is read from top to bottom. Based on this, when you call the Read() method, it reads the first node, moves to the next, and so on until it reaches the end of the file. While reading the file, every time this method reaches a node, you can find out what type of node it is by checking the XmlTextReader.NodeType property. This can help you take specific action if the reached node meets a certain criterion.

As reviewed in our introductions to XML, each node has a name and possibly a value. You can find out the name of a node by checking the XmlTextReader.Name property. To know the value of a node, retrieve its XmlTextReader.Value property. 

Practical Learning Practical Learning: Reading the Elements of an XML File

  1. In the Tools window, click OpenFileDialog and click the form
  2. While the openFileDialog1 control is still selected, in the Properties window, change its characteristics as follows:
    DefaultExt: xml
    Filter: Repair Orders (*.xml)|*.xml|All files (*.*)|*.*"

    Title:  Open Existing Repair Order
  3. Double-click the Open an Existing Order button and implement its Click event as follows:
     
    void BtnOpenClick(object sender, System.EventArgs e)
    {
    	XmlTextReader rdrRepairOrder = null;
    				 
    	try {
    		if( this.openFileDialog1.ShowDialog() == DialogResult.OK )
    		{
    		 	rdrRepairOrder = new XmlTextReader(this.openFileDialog1.FileName);
    
    			 // Scan the XML file
    			 while( rdrRepairOrder.Read() )
    			 {
    				 // every time you find an element, find out what type it is
    				 // If you find text, put it in the combo box' list
    			 if( (XmlNodeType.Element != 0) && (rdrRepairOrder.Name == "CustomerName") )
    			 {
    			 this.txtCustomerName.Text = rdrRepairOrder.ReadElementString("CustomerName");
    			 this.txtAddress.Text = rdrRepairOrder.ReadElementString("Addres");
    			 this.txtCity.Text = rdrRepairOrder.ReadElementString("City");
    			 this.txtState.Text = rdrRepairOrder.ReadElementString("State");
    			 this.txtZIPCode.Text = rdrRepairOrder.ReadElementString("ZIPCode");
    			 this.txtMake.Text = rdrRepairOrder.ReadElementString("Make");
    			 this.txtModel.Text	= rdrRepairOrder.ReadElementString("Model");
    			 this.txtCarYear.Text	= rdrRepairOrder.ReadElementString("CarYear");
    			 this.txtProblem.Text = rdrRepairOrder.ReadElementString("ProbDescription");
    			 this.txtPartName1.Text	= rdrRepairOrder.ReadElementString("PartName1");
    			 this.txtUnitPrice1.Text	= rdrRepairOrder.ReadElementString("UnitPrice1");
    			 this.txtQuantity1.Text	= rdrRepairOrder.ReadElementString("Quantity1");
    			 this.txtSubTotal1.Text	= rdrRepairOrder.ReadElementString("SubTotal1");
    			 this.txtPartName2.Text	= rdrRepairOrder.ReadElementString("PartName2");
    			 this.txtUnitPrice2.Text	= rdrRepairOrder.ReadElementString("UnitPrice2");
    			 this.txtQuantity2.Text = rdrRepairOrder.ReadElementString("Quantity2");
    			 this.txtSubTotal2.Text	= rdrRepairOrder.ReadElementString("SubTotal2");
    			 this.txtPartName3.Text	= rdrRepairOrder.ReadElementString("PartName3");
    			 this.txtUnitPrice3.Text	= rdrRepairOrder.ReadElementString("UnitPrice3");
    			 this.txtQuantity3.Text	= rdrRepairOrder.ReadElementString("Quantity3");
    			 this.txtSubTotal3.Text = rdrRepairOrder.ReadElementString("SubTotal3");
    			 this.txtPartName4.Text	= rdrRepairOrder.ReadElementString("PartName4");
    			 this.txtUnitPrice4.Text	= rdrRepairOrder.ReadElementString("UnitPrice4");
    			 this.txtQuantity4.Text	= rdrRepairOrder.ReadElementString("Quantity4");
    			 this.txtSubTotal4.Text	= rdrRepairOrder.ReadElementString("SubTotal4");
    			 this.txtPartName5.Text	= rdrRepairOrder.ReadElementString("PartName5");
    			 this.txtUnitPrice5.Text	= rdrRepairOrder.ReadElementString("UnitPrice5");
    			 this.txtQuantity5.Text	= rdrRepairOrder.ReadElementString("Quantity5");
    			 this.txtSubTotal5.Text	= rdrRepairOrder.ReadElementString("SubTotal5");
    			 this.txtJobPerformed1.Text= rdrRepairOrder.ReadElementString("Job1");
    			 this.txtJobPrice1.Text	= rdrRepairOrder.ReadElementString("JobPrice1");
    			 this.txtJobPerformed2.Text= rdrRepairOrder.ReadElementString("Job2");
    			 this.txtJobPrice2.Text	= rdrRepairOrder.ReadElementString("JobPrice2");
    			 this.txtJobPerformed3.Text= rdrRepairOrder.ReadElementString("Job3");
    			 this.txtJobPrice3.Text	= rdrRepairOrder.ReadElementString("JobPrice3");
    			 this.txtJobPerformed4.Text= rdrRepairOrder.ReadElementString("Job4");
    			 this.txtJobPrice4.Text	= rdrRepairOrder.ReadElementString("JobPrice4");
    			 this.txtJobPerformed5.Text = rdrRepairOrder.ReadElementString("Job5");
    			 this.txtJobPrice5.Text	= rdrRepairOrder.ReadElementString("JobPrice5");
    			 this.txtTotalParts.Text	= rdrRepairOrder.ReadElementString("TotalPart");
    			 this.txtTotalLabor.Text	= rdrRepairOrder.ReadElementString("TotalLabor");
    			 this.txtTaxRate.Text	= rdrRepairOrder.ReadElementString("TaxRate");
    			 this.txtTaxAmount.Text	= rdrRepairOrder.ReadElementString("TaxAmount");
    			 this.txtTotalOrder.Text	= rdrRepairOrder.ReadElementString("TotalOrder");
    		 this.txtRecommendations.Text = rdrRepairOrder.ReadElementString("Recommendation");
    			 }       
    		 }
    	 }
        }
        catch(XmlException )
       {
    	 MessageBox.Show("The file name you provided is not valid");
       }
        finally
       {
    	 rdrRepairOrder.Close();
       }
    }		
  4. Execute the application and try opening the previous order

Details on XML Reading and Writing

 

Indentation

Consider a valid XML file such as the following, opened in Notepad:

If you were asked to examine this file, you can see that its crowded words make it difficult to read. To include a white space when writing to the file, you can call the XmlTextWriter.WriteWhiteSpace() method. Its syntax is:

public override void WriteWhitespace(string ws);

Besides white spaces, indentation consists of setting empty space on the left of child nodes to make the file easier to read. Based on this, indentation is not a requirement but a convenience. While the WriteWhiteSpace() method allows you to explicitly create a white space in the file, the XmlTextWriter class is equipped with the Formatting property. This property is a value of the Formatting enumerator. The Formatting enumerator has two members. The Indented value ensures that each child node would be indented from its parent.

If you manually create an XML file, whether using Notepad, Visual Studio, or another text editor, you can indent the nodes as you see fit. To let the compiler know that you want the nodes to be indented, assign the Formatting.Indented member to its Formatting property. Here is an example:

void BtnSaveClick(System.object sender, System.EventArgs e)
{
	 XmlTextWriter xmlTxtWriter = new XmlTextWriter("Videos.xml", Encoding.UTF8);
	 xmlTxtWriter.Formatting = Formatting.Indented;

	 xmlTxtWriter.Flush();
	 xmlTxtWriter.Close();
}

If you manually create an XML file, you can specify the number of empty spaces on the left of a node by pressing the Space bar a few times before typing the node. Most people use 2 or 4 characters for the indentation. If you are programmatically creating the file, to specify the number of characters that should be used during indentation, assign the desired integer to the XmlTextWriter.Indentation property. If you don't use this property, the compiler would use two characters. You can also find out the number of characters used for indentation by retrieving the value of this property.

As mentioned above, indentation consists of entering white spaces on the left of child nodes. Instead of empty spaces, if you want to use another character, assign it to the XmlTextWriter.IndentChar property.

Practical Learning Practical Learning: Indenting XML Nodes

  1. To control the indentation when the file is saved, change the top section of the Click event of the Save button as follows:
     
    void BtnSaveClick(System.object sender, System.EventArgs e)
    {
    	 . . . No Change
    
    	 if( this.saveFileDialog1.ShowDialog() == DialogResult.OK )
    	 {
    		 XmlTextWriter wtrRepairOrder = new XmlTextWriter(this.saveFileDialog1.FileName, Encoding.UTF8);
    		 wtrRepairOrder.Formatting    = Formatting.Indented;
    		 wtrRepairOrder.Indentation   = 4;
    
    		. . . No Change
    	 
    	 
    	 }
    }
  2. Return to the Design view of the form and double-click the Close button
  3. Implement it as follows:
     
    void BtnCloseClick(object sender, System.EventArgs e)
    {
    	Close();
    }
  4. Execute the application
  5. Create, calculate, and save a new repair order
  6. Close the form
 

Previous Copyright 2005 FunctionX, Inc.