Friday 16 December 2011

How to read XML from a file by using Visual C#

This article describes how to use the XmlTextReader class to read Extensible Markup Language (XML) from a file. XmlTextReaderprovides direct parsing and tokenizing of XML and implements the XML 1.0 specification as well as the namespaces in the XML specification from the World Wide Web Consortium (W3C). This article provides fast, tokenized stream access to XML rather than using an object model such as the XML Document Object Model (DOM).

Requirements


The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:

  • Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET


This article assumes that you are familiar with the following topics:

  • XML terminology

  • Creating and reading an XML file


How to read XML from a file


This example uses a file named Books.xml. You can create your own Books.xml file or use the sample file that is included with the .NET Software Development Kit (SDK) QuickStarts in the following folder:
\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Samples\QuickStart\Howto\Samples\Xml\Transformxml\Cs

You must copy Books.xml to the \Bin\Debug folder, which is located under the folder in which you create this project. Books.xml is also available for download.

  1. Start Visual Studio 2005 or Visual Studio .NET.

  2. Create a new Visual C# Console Application. continue through these steps to build the application.

  3. Make sure that the project contains a reference to the System.Xml.dll assembly.

  4. Specify the using directive on the System.Xml namespace so that you are not required to qualify XmlTextReader declarations later in your code. You must use the usingdirective before any other declarations.


    using System.Xml;



  5. Create an instance of an XmlTextReader object, and populate it with the XML file. Typically, the XmlTextReader class is used if you need to access the XML as raw data without the overhead of a DOM; thus, the XmlTextReader class provides a faster mechanism for reading XML. The XmlTextReader class has different constructors to specify the location of the XML data. The following code creates an instance of the XmlTextReaderclass and loads the Books.xml file. Add the following code to the Main procedure of Class1.


    XmlTextReader reader = new XmlTextReader ("books.xml");



  6. Read through the XML. (Note that this step demonstrates an outer "while" loop, and the next two steps demonstrate how to use that loop to read the XML.) After you create the XmlTextReader object, use the Read method to read the XML data. The Read method continues to move through the XML file sequentially until it reaches the end of the file, at which point the Readmethod returns a value of "False."


    while (reader.Read()) 
    {
    // Do some work here on the data.
    Console.WriteLine(reader.Name);
    }
    Console.ReadLine();



  7. Inspect the nodes. To process the XML data, each record has a node type that can be determined from the NodeType property. The Name and Value properties return the node name (the element and attribute names) and the node value (the node text) of the current node (or record). The NodeTypeenumeration determines the node type. The following sample code displays the name of the elements and the document type. Note that this sample ignores element attributes.


    while (reader.Read()) 
    {
    switch (reader.NodeType)
    {
    case XmlNodeType.Element: // The node is an element.
    Console.Write("<" + reader.Name);
    Console.WriteLine(">");
    break;
    case XmlNodeType.Text: //Display the text in each element.
    Console.WriteLine (reader.Value);
    break;
    case XmlNodeType. EndElement: //Display the end of the element.
    Console.Write("</" + reader.Name);
    Console.WriteLine(">");
    break;
    }
    }



  8. Inspect the attributes. Element node types can include a list of attribute nodes that are associated with them. The MovetoNextAttribute method moves sequentially through each attribute in the element. Use the HasAttributes property to test whether the node has any attributes. The AttributeCountproperty returns the number of attributes for the current node.


    while (reader.Read()) 
    {
    switch (reader.NodeType)
    {
    case XmlNodeType.Element: // The node is an element.
    Console.Write("<" + reader.Name);

    while (reader.MoveToNextAttribute()) // Read the attributes.
    Console.Write(" " + reader.Name + "='" + reader.Value + "'");
    Console.WriteLine(">");
    break;
    case XmlNodeType.Text: //Display the text in each element.
    Console.WriteLine (reader.Value);
    break;
    case XmlNodeType. EndElement: //Display the end of the element.
    Console.Write("</" + reader.Name);
    Console.WriteLine(">");
    break;
    }
    }



  9. Save and close your project.



Complete code listing




using System;
using System.Xml;

namespace ReadXMLfromFile
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static void Main(string[] args)
{
XmlTextReader reader = new XmlTextReader ("books.xml");
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
Console.Write("<" + reader.Name);
Console.WriteLine(">");
break;
case XmlNodeType.Text: //Display the text in each element.
Console.WriteLine (reader.Value);
break;
case XmlNodeType.EndElement: //Display the end of the element.
Console.Write("</" + reader.Name);
Console.WriteLine(">");
break;
}
}
Console.ReadLine();
}
}
}



Sample output




<bookstore>
<book>
<title>
The Autobiography of Benjamin Franklin
</title>
<author>
<first-name>
Benjamin
</first-name>
<last-name>
Franklin
</last-name>
</author>
<price>
8.99
</price>
</book>
<book>
<title>
The Confidence Man
</title>
<author>
<first-name>
Herman
</first-name>
<last-name>
Melville
</last-name>
</author>
<price>
11.99
</price>
</book>
<book>
<title>
The Gorgias
</title>
<author>
<name>
Plato
</name>
</author>
<price>
9.99
</price>
</book>
</bookstore>



Troubleshooting


When you test the code, you may receive the following exception error message: Unhandled Exception: System.Xml.XmlException: Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it.

Additional information: System error. The exception error occurs on the following line of code: While


(reader.Read())



The exception error is caused by an invalid processing instruction. For example, the processing instruction may contain extraneous spaces. The following is an example of an invalid processing instruction:


<?xml version='1.0' ?>



This xml tag has a space preceding the ‘<’ bracket. Remove the preceding whitespace to resolve the error.

No comments:

Post a Comment