Section 2.7. Visualize XML Data


2.7. Visualize XML Data

As noted in the previous lab, the debugger allows you to examine text, HMTL, and XML. In this lab you'll create an XML document, read it into a program, and then examine the XML data using the XML Visualizer.


Note: Visual Studio 2005 provides an XML Visualizer for examining XML data.

2.7.1. How do I do that?

Create a new Console application named ExamineXML. Add an XML file to the project by right-clicking the project and choosing Add New Item XML File. Name the new XML file BookList.xml and populate it with the valid XML data shown in Example 2-2.

Example 2-2. Valid XML data sample
<?xml version="1.0" ?>  <Books>     <book>         <BookName>Programming C#</BookName>         <Author>Jesse Liberty</Author>         <Publisher>O'Reilly Media</Publisher>     </book>     <book>         <BookName>Programming ASP.NET</BookName>         <Author>Jesse Liberty</Author>         <Author>Dan Hurwitz</Author>         <Publisher>O'Reilly Media</Publisher>     </book>     <book>         <BookName>Visual C# Notebook</BookName>         <Author>Jesse Liberty</Author>         <Publisher>O'Reilly Media</Publisher>     </book> </Books>

Next, write a Test method to read the XML file using a StreamReader object and concatenate all the strings read into a single long XML string, as shown in Example 2-3.

Example 2-3. Reading the XML file
using System; using System.IO; using System.Xml;     namespace ExamineXML {    class Program    {       static void Main(string[  ] args)       {           using (StreamReader reader = File.OpenText(@"..\..\BookList.xml"))           {             string completeXML = reader.ReadToEnd( );             Console.WriteLine("Received: {0}", completeXML);          }           }    } }

Place a breakpoint on the last line of the Main method. When you hit the breakpoint, hover your mouse cursor over the complete XML string. The string will be shown, and at the end of the display will be a magnifying glass. Click the magnifying glass and you'll have the opportunity to pick the Visualizer you want to use, as shown in Figure 2-20 (the arrow in Figure 2-20 points to the magnifying glass).

Figure 2-20. Picking the Visualizer


You can examine the complete string in the debugger using either the Text Visualizer or, more interestingly, the XML Visualizer, as shown in Figure 2-21.

Figure 2-21. The XML Visualizer



Warning: The XML Visualizer will not work as shown if encoding = "utf-8" is specified in the XML file.

2.7.2. What about . . .

...custom (proprietary) data?

The framework provides you with classes to enable you to create your own visualizers. To see how, read the MSDN article "How to: Create a Debugger Visualizer Using Visual Studio 2005."

2.7.3. Where can I learn more?

Run a search in Google with the words "Visual Studio Visualizer." When I ran that search I found more than 700 hits, many of which were projects that demonstrated how to create new visualizers, such as image visualizers. This is a rapidly emerging technology.



Visual C# 2005(c) A Developer's Notebook
Visual C# 2005: A Developers Notebook
ISBN: 059600799X
EAN: 2147483647
Year: 2006
Pages: 95
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net