Home

Using an XML File

 

An XML File in a Grid

After creating an XML file, you can use it. One way you can do this is to display it in a grid-based control. If you create or open an XML file using Visual Studio .NET, the window you use to edit the file is equipped with a property page that allows you to view the result of the file. This window can be accessed by clicking the Data button. In future lessons, we will see that you can also use the DataGrid control to display the contents of an XML file.

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

  • To see a display of the file's content, in the lower section of the Parts.xml tab, click the Data button
     
    XML in a Data Sheet

 

An XML File in a Browser

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.

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: Displaying an XML File in a Browser

  1. Open Windows Explorer or My Computer and display the contents of the folder that contains the current project
    Double-click the Employees.xml file
     
    An XML File Displaying in a Browser
  2. Return to Visual Studio .NET
    To create a CSS file, on the main menu, click File -> New -> File...
  3. In the Templates list, click Style Sheet and click Open
  4. To save the file, on the the Standard toolbar, click the Save button
  5. Make sure the debug sub-folder of the bin folder inside the folder of the current project is selected; otherwise, select it
    Change the file name to cpap
  6. Right-click an empty area in the window and click Add Style Rule...
  7. Click the Element combo box and type EmplNumber
     
    Add Style Rule
  8. Click OK
  9. In the same way add a FirstName Element, followed by a LastName and a HourlySalary Elements
  10. Right-click under EmplNumber and click Build Style...
  11. With the Family radio button selected, click the ellipsis button on the right side
  12. In the Font Picker list box, click Garamond and click the select button
  13. In the Font Picker list box, double-click Georgia
  14. In the same way, add the Times New Roman font
  15. In the Generic Fonts combo box, select Serif and click the select button
     
    Font Picker
  16. Click OK
  17. In the Font Attributes section, click the arrow of the Color combo box and select White
  18. In the Size section, on the right side of the Specific radio button, type 16 and select pt in the right combo box
  19. In the Bold section, click the combo box on the right side of Absolute and select Bold
     
    Style Builder
  20. In the left frame, click Background
  21. On the right side of Color, click the ellipsis button
  22. In the Web Palette tab, click a dark red color
     
    Color Picker
  23. Click OK
     
  24. In the left frame, click Layout
  25. In the Display combo box, select As A Block Element
  26. Click OK
  27. Complete the style sheet file as follows:
     
    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;
    }
  28. To open the Employees.xml file, on the main menu, click File -> Open -> File...
  29. Change the Files of Type to Web Files
  30. Locate the folder of the current project. Double-click the bin folder to display it in the Save In combo box
  31. Select Employees.xml and click Open
  32. Click the first line, press End and press Enter
  33. Type <?xml:stylesheet href="cpap.css" type="text/css" ?>
  34. Close the Employees.xml
  35. When asked whether you want to save it, click Yes
  36. Close the cpap.css
  37. When asked whether you want to save it, click Yes
  38. Return to the browser and refresh it
     
    An XML File Using Style Sheet

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 use 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:

Overloads Public Overridable Sub Load(ByVal filename As String)

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. We will also explore its properties and methods on time.

 

Previous Copyright © 2004-2010 FunctionX, Inc. Next