Home

Using an XML File

 

An XML File in a Browser

After creating an XML file, one way you can use it is to display it in a grid-based window such as the DataGrid control or a spreadsheet. Another way you can display an XML file is in a browser. To do this, if you see the file in Windows Explorer, in My Computer, or in My Documents, you can double-click it.

 

Practical Learning Practical Learning: Displaying an XML File in a Browser

  1. Open Windows Explorer or My Computer and display the contents of the IntroXML folder that contains the files created in the previous lesson
  2. Double-click the Employees.xml file
     
  3. Return to Notepad

Using a Style Sheet to Display an XML File in a Browser

When an XML file is displayed in a browser, it appears in a format that would be unclear to most people. If you want the XML code to display as if it were HTML, you can create a cascading style sheet that would format the tags and display the text as you prefer.

 

Practical Learning Practical Learning: Using a Style Sheet

  1. To create a CSS file, start another instance of Notepad and type the following in it:
     
    EmplNumber
    {
    	display: block;
    	font-weight: bold;
    	font-size: 16pt;
    	color: white;
    	font-family: Garamond, Georgia, 'Times New Roman' , Serif;
    	background-color: #990000;
    }
    FirstName
    {
    	font-size: 10pt;
    	font-family: Verdana, Tahoma, Arial, Sans-Serif;
    	background-color: white;
    }
    LastName
    {
    	font-size: 10pt;
    	font-family: Verdana, Tahoma, Arial, Sans-Serif;
    	background-color: white;
    }
    HourlySalary
    {
    	font-size: 10pt;
    	color: #ff0066;
    	font-family: Verdana, Tahoma, Arial, Sans-Serif;
    	background-color: white;
    	display: block;
    }
  2. Save the file as empl.css in your IntroXML folder
     
  3. To open the Employees.xml file, on the main menu, click File -> Open...
  4. Change the Files of Type to All Files
  5. Select Employees.xml
     
  6. Click Open
  7. Click the first line, press End and press Enter
  8. Type <?xml:stylesheet href="empl.css" type="text/css" ?>
  9. Close this instance of Notepad
  10. When asked whether you want to save the changes to the Employees.xml file, click Yes
  11. Return to the browser and refresh it
     
    An XML File Using Style Sheet
  12. Close the browser

Opening an XML File

Instead of displaying the contents of an XML file in a browser, you may want to open it as a document. To do this, you can call the XmlDocument.Load() method. It comes in four versions. One of the versions of this method receives the file name or path to an XML file as argument. Its syntax is:

public virtual void Load(string filename);

The name of the XML file or its path must be passed to this version of the Load() method. If the method succeeds, it opens the file. If it doesn't, the compiler throws an XmlException type of exception.

Reading XML Text

Another way you can explore an XML file consists of reading it. To support reading an XML file, the .NET Framework provides the XmlTextReader.

 

Previous Copyright © 2005-2016, FunctionX Next