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 getting support for Beta versions, see the documentation included in the beta product file, or view the site you download this version.
For Microsoft Visual Basic .NET versions of this article, see
CHS301232.
This task content
summary
Require how to read XML data from URL Sample Output Reference
Summary How to use this article
The XMLTextReader class reads the extended tag language (XML) from the Unified Resource Locator (URL). Flow information can have a variety of sources, such as from servers, files or
The byte stream of the TextReader class.
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 reads XML URL and creates an XML endpoint
Back to top
How to read an XML data from a URL This example uses a file named books.xml. You can create your own books.xml file or use the sample file included in the .NET Software Development Kit (SDK). You can also download this file; see this article for information on downloading location.
The first article of the reference section.
Copy the books.xml file to the / Inetpub / wwwroot folder on your computer. Open Visual Studio .NET. New Visual C # .NET Console Application. You can go to the full code list section, or you can continue to perform these steps to generate an application. Specify Using instructions on the System.xml namespace so that you don't need to define the XMLTextReader class in the code. The USING instruction must be before any other declaration. Using system.xml; retrieve XML stream via URL. The flow is used to provide independence between the devices, so if the source of the stream changes, the procedure is not required to change. Declare a constant to http: //localhost/books.xml URL. In the next step, this constant is used for XMLTextReader. Add the following code example to this default class MAIN process: string urlstring = "http://localhost/books.xml"; create an instance of the XMLTextReader class and specify the URL. Typically, use XMLTextReader if you need to use XML as raw data without generating a document object model (DOM) overhead; therefore, XMLTextReader provides a mechanism for reading XML faster. The XMLTextReader class uses a different constructor to specify the location of the XML data. The following code creates an instance of the XMLTextReader object and passes the URL to the constructor: XMLTextReader Reader = New XMLTextReader (UrlString); read all XML data. (Note that this step shows a basic external "While" loop, how to use this loop and read XML.) After loading, XMLTextReader will continue to read XML data continuously and use the READ method to get the next record. If there is no record, the read method will return false. While (Reader.Read ())
{
// do some work here on the data.console.writeline (ready.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;
}
} Generate and run your project.
Back to top
Complete code list
Using system;
USING SYSTEM.XML;
Namespace READXMLFROMURL
{
///
/// summary description for class1.
/// summary>
Class class1 {
Static void main (string [] args)
{
String urlstring = "http://localhost/books.xml";
XmlTextReader Reader = New XMLTextReader (Urlstring);
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;
}
}
}
}
}
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, see the following Microsoft .NET Framework Class library documentation:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemXMLXMLREADERCLASSTOPIC.ASP for how to use
XmlReader reads more information about XML data, see the Microsoft .NET Framework Developer's Guide Document:
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 KB307643 KBAUDDEVELOPER