Chapter 17. XML

XML (Extensible Markup Language) is a simple, portable, and flexible way to represent data in a structured format. XML is used in a myriad of ways, from acting as the foundation of web-based messaging protocols like SOAP, to being one of the more popular ways to store configuration data (such as the web.config, machine.config, or security.config files in the .NET Framework). Microsoft recognized the usefulness of XML to developers and has done a nice job of giving the developer choices around the tradeoffs one encounters when using XML. Sometimes you want to simply run though an XML document looking for a value in a read-only cursor-like fashion, and other times you need to be able to randomly access various pieces of the document. Microsoft provides classes like XmlTextReader and XmlTextWriter for lighter access and XmlDocument for full DOM (Document Object Model) processing support. It is likely that if you use .NET you will be dealing with XML to one degree or another, and in this chapter we explore some of the uses for XML and XML-based technologies like XPath and XSLT, as well as explore topics like validation of XML and transformation of XML to HTML.

17.1. Reading and Accessing XML Datain Document Order

Problem

You need to read in all the elements of an XML document and obtain information about each element, such as its name and attributes.

Solution

Create an XMLTextReader and use its Read method to process the document:

using System;
using System.Xml;

// ...

public static void Indent(int level)
{
    for (int i = 0; i < level; i++)
      Console.Write(" ");
}

public static void AccessXML( )
{
    string xmlFragment = "<?xml version='1.0'?>" +
    "<!-- My sample XML -->" +
    "<?pi myProcessingInstruction?>" +
    "<Root>" + 
    "<Node1 nodeId='1'>First Node</Node1>" +
    "<Node2 nodeId='2'>Second Node</Node2>" +
    "<Node3 nodeId='3'>Third Node</Node3>" +
    "</Root>"; 

    XmlTextReader reader = new XmlTextReader(xmlFragment, 
         XmlNodeType.Element, null);
    int level = 0;

    while (reader.Read( ))
    {
        switch (reader.NodeType) 
        {
            case XmlNodeType.CDATA:
                Indent(level);
                Console.WriteLine("CDATA: {0}", reader.Value);
                break;
            case XmlNodeType.Comment :
                Indent(level);
                Console.WriteLine("COMMENT: {0}", reader.Value);
                break;
            case XmlNodeType.DocumentType :
                Indent(level);
                Console.WriteLine("DOCTYPE: {0}={1}", 
                  reader.Name, reader.Value);
                break;
            case XmlNodeType.Element :
                Indent(level);
                Console.WriteLine("ELEMENT: {0}", reader.Name);
                level++;
                while(reader.MoveToNextAttribute( ))
                {
                    Indent(level);
                    Console.WriteLine("ATTRIBUTE: {0}='{1}'",
                        reader.Name, reader.Value);
                }
                break;
            case XmlNodeType.EndElement :
                level--;
                break;
            case XmlNodeType.EntityReference :
                Indent(level);
                Console.WriteLine("ENTITY: {0}", reader.Name);
                break;
            case XmlNodeType.ProcessingInstruction :
                Indent(level);
                Console.WriteLine("INSTRUCTION: {0}={1}", 
                  reader.Name, reader.Value);
                break;
            case XmlNodeType.Text :
                Indent(level);
                Console.WriteLine("TEXT: {0}", reader.Value);
                break;
            case XmlNodeType.XmlDeclaration :
                Indent(level);
                Console.WriteLine("DECLARATION: {0}={1}", 
                  reader.Name, reader.Value);
                break;
        }       
    }
    reader.Close( );
}

This code dumps the XML document in a hierarchical format:

DECLARATION: xml=version='1.0'
COMMENT:  My sample XML
INSTRUCTION: pi=myProcessingInstruction
ELEMENT: Root
 ELEMENT: Node1
  ATTRIBUTE: nodeId='1'
  TEXT: First Node
 ELEMENT: Node2
  ATTRIBUTE: nodeId='2'
  TEXT: Second Node
 ELEMENT: Node3
  ATTRIBUTE: nodeId='3'
  TEXT: Third Node

Discussion

Reading existing XML and identifying different node types is one of the fundamental actions that you will need to perform when dealing with XML. The code in the Solution shows how to create an XmlTextReader from either a string or from a stream, and then iterate over the nodes while recreating the formatted XML for output to the console window.

See Also

See the “XmlTextReader Class,” “XmlNodeType Enumeration,” and “StringReader Class” topics in the MSDN documentation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.81.43