This article discusses a Beta version of Microsoft products. The information in this article is provided as "as", if there is any change without notice.
For this Beta product, Microsoft does not provide formal product support. For information on gain support for Beta versions, see the documentation included in the beta product file or view the site you downloaded.
For Microsoft Visual Basic .NET versions of this article, see
CHS301225.
This task content
Summary
Require how to read XML data complete code list example output reference
Summary How to use this article
The XMLTextReader class reads the Scalable Markup Language (XML) from the file.
XMLTextReader can perform syntax analysis and tags of XML, and implement XML 1.0 specification and namespaces in the WWW Federation (W3C) XML specification. This article provides fast, marking stream access to XML, rather than using an object model such as an XML document object model (DOM).
Back to top
The following table summarizes the recommended hardware, software, network architecture, and service pack required:
Microsoft Visual Studio .NET This article assumes that you are familiar with the following topics:
XML terminology creates and read XML files
Back to top
How to read the XML this example from a file Using a file called Books.xml. You can create your own books.xml file, or use the .NET Software Development Kit (SDK) to quickly get started with the sample file included in the following folder.
/ Program Files / Microsoft Visual Studio .NET / FRAMEWORKSDK / Samples / QuickStart / HOWTO / SAMPLES / XML / TransformXML / CS must copy the books.xml file to the / bin / debug folder, which is located in the file where the project is created. The lower side of the clip. You can also download books.xml; for download, see
Reference section.
Open Visual Studio .NET. New Visual C # .NET Console Application. You can read the full code list directly, or continue to perform the following steps to generate an application. Make sure the project contains references to System.xml.dll assembly. Specify Using instructions on the System.xml namespace so that you don't need to define XMLTextReader declaration in your code. The USING instruction must be before any other declaration. Using System.xml; Create an instance of an XMLTextReader object and populates the instance using the XML file. Typically, if you need to access XML as raw data without producing a DOM overhead, use an XMLTextReader class; therefore, the XMLTextReader class provides a more quickly read XML mechanism. The XMLTextReader class uses a different constructor to specify the location of the XML data. The following code creates an instance of an XMLTextReader object and loads the books.xml file. Add the following code to the main process of the Class1: XMLTextReader Reader = New XMLTextReader ("Books.xml"); read all XML data. (Note that this step demonstrates an external "While" loop and demonstrates how to use this loop to read XML.) After creating the XMLTextReader object, use the Read method to read XML data. The READ method continues sequentially read the XML file until the end of the file is reached, at which time the READ method returns "false" value. While (Reader.Read ()) {
// Do some work here on the data.
Console.writeLine (Reader.Name);
}
Console.readline (); check node. To process XML data, each record should have a node type that can be determined by NodetyPE properties. Name and Value Attributes Returns the node name (element and attribute name) of the current node (or record) and node value (node text). NodeType enumerates determines the node type. The following code example shows the name and document type of the element. Note that this example ignores the element properties. While (Reader.Read ())
{
Switch (Reader.NodeType)
{
Case XMLNodetype.Element: // The node is an element.
Console.write ("<" Reader.Name);
Console.writeline (">");
Break;
Case XMLNodType.Text: // Display The Text in Each Element.
Console.writeline (Reader.Value);
Break;
Case XMLNodType. endElement: // display the end of the element.
Console.write ("" ready.name;
Console.writeline (">");
Break;
}
} Check the properties. Element node types can include a series of attribute nodes associated therewith. The MoveTonextAttribute method is moved in each attribute of the element. Use the Hasattribute attribute to detect whether the node has any properties. AttributeCount properties Returns the number of properties of 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.write (">");
Console.writeline (">");
Break;
Case XMLNodType.Text: // Display The Text in Each Element.
Console.writeline (Reader.Value);
Break;
Case XMLNodType. endElement: // display the end of the element.
Console.write ("" ready.name;
Console.writeline (">");
Break;
}
} Save and shut down the project.
Back to top
Complete code list
Using system;
USING SYSTEM.XML;
Namespace ReadXmlFromfile
{
///
/// 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 XMLNodType.Text: // Display The Text in Each Element.
Console.writeline (Reader.Value);
Break;
Case XMLNodetype.Endelement: // Display the end of the element.
Console.write ("" ready.name;
Console.writeline (">");
Break;
}
}
Console.readline ();
}
}
}
Back to top
Example output
The Autobiography of Benjamin Franklin
title>
Benjamin
first-name>
Franklin
Last-Name>
author>
8.99
price>
book>
The Confidence Man
title>
Herman
first-name>
Melville
Last-Name>
author>
11.99
price>
book>
The gorgias
title>
Plato
name>
author>
9.99
price>
book>
bookstore>
Back to top
Refer to the following files to download from the Microsoft Download Center:
Download Books.xml now For more information, see "XML IN .NET: .NET Framework XML Classes and C # Offer Simple, Scalable Data Manipulation" (. NET XML: .NET Framework XML class and C # provide simple, Zoom data operation) One article, this article is
In MSDN Magazine, you can access from the following Microsoft Web site:
Http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnmag01/html/xml0101.asp
For more information on the XMLReader class, please visit the following .NET Framework Class Library Web Site:
http://msdn.microsoft.com/library/dotnet/cpref/frlrfsystemxmlxmlreadERCLASTOPIC.HTM for how to use
XmlReader reads more information about XML data, see the Microsoft .NET Framework Developer Document Guide documentation below:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguidnf/html/cpconreadingXMLDataUsingXMLReader.asp
Back to top
The information in this article applies to:
Microsoft Visual C # .NET Beta 2
Recent Update: 2004-8-13 (1.2) Keywords: KBDownload Kbhowto KbhowTomaster KB307548 KBAUDDEVELOPER