บทความนี้อธิบายวิธีใช้นี้
XmlTextReaderคลาอ่าน Extensible Markup ภาษา (XML) จากกระแสข้อมูล สตรีมอาจมาจากหลากหลายแหล่ง เช่นกระแสข้อมูลแบบไบต์จากเซิร์ฟเวอร์ แฟ้ม หรือยัง
textreaderคลาสที่
ความต้องการ
รายการต่อไปนี้แสดงฮาร์ดแวร์ที่แนะนำ ซอฟต์แวร์ โครงสร้างพื้นฐานของเครือข่าย และ service pack ที่คุณต้องการ:
- Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 ขั้นสูงเซิร์ฟเวอร์ หรือเซิร์ฟเวอร์ Windows NT 4.0
- Microsoft Visual Studio .NET
บทความนี้อนุมานว่า คุณไม่คุ้นเคยกับหัวข้อต่อไปนี้:
- คำศัพท์ xml
- การสร้างและอ่าน XML
วิธีการที่อ่านข้อมูล XML จากตัวกระแสข้อมูล
- เปิด Visual Studio .NET
- สร้างใหม่ Microsoft Visual Basic (VB) หรือ Microsoft Visual c# คอนโซลโปรแกรมประยุกต์
หมายเหตุ:: ขั้นตอนต่อไปนี้การใส่คำอธิบายโดยละเอียดเกี่ยวกับวิธีการสร้างโปรแกรมประยุกต์ คุณสามารถจะไปตรงขั้นตอนที่ 10 ซึ่งมีให้รหัสที่เสร็จสมบูรณ์ - ตรวจสอบให้แน่ใจว่า โครงการที่ประกอบด้วยการอ้างอิงไปยังSystem.XmlและSystem.IOnamespace
- ใช้คำสั่งนำเข้าXMLnamespace ดังนั้นคุณไม่จำเป็นต้องกำหนดคุณสมบัติXmlTextReaderdeclarations ใน namespace นั้นในภายหลังในรหัสของคุณ คุณต้องใช้คำสั่งนำเข้าก่อนที่จะ declarations อื่น ๆ ต่อไปนี้:
โค้ด visual Basic .NET
Imports System.Xml
Imports System.IO
แสดงรหัส c#
using System.Xml;
using System.IO;
- สร้าง หรือเรียกกระแสข้อมูล XML กระแสข้อมูลแบบไม่มีการแสดงของการป้อนข้อมูล หรืออุปกรณ์แสดงผลที่เป็นแหล่งที่มาของ หรือปลายทางสำหรับ ข้อมูล (ในกรณีนี้ ข้อมูล XML) abstract คุณสามารถเขียน และอ่านจากกระแสข้อมูลแบบ ซึ่ง visualized มากที่สุดเป็นลำดับไบต์
กระแสข้อมูลถูกใช้เพื่อให้ independence จากอุปกรณ์ และต้องมีการเปลี่ยนแปลงโปรแกรมดังนั้น ถ้า ตัวอย่างเช่น การเปลี่ยนแปลงของแหล่งที่มาของกระแสข้อมูล มีวิธีที่แตกต่างกันเพียงไม่กี่เพื่อสร้างกระแสข้อมูลสำหรับการXmlTextReaderคลาสที่ เลือกตัวอย่างรหัสต่อไปนี้เพื่อเพิ่มลงในกระบวนการหลักของโมดูลที่เป็นค่าเริ่มต้นอย่างใดอย่างหนึ่ง:- ตัวอย่างรหัสที่ใช้ในการStringReaderวัตถุ:
กระบวนการStringReaderวัตถุอ่านอักขระจากสายอักขระ และใช้ค่าสายอักขระในระหว่างการดำเนินการvisual Basic .NET รหัส
Dim stream as System.IO.StringReader
stream = new StringReader("<?xml version='1.0'?>" & _
"<!-- This file is a book store inventory. -->" & _
"<bookstore>" & _
" <book genre=""autobiography"" ISBN=""1-861003-11-0"">" & _
" <title>The Autobiography of Benjamin Franklin</title>" & _
" <author>" & _
" <first-name>Benjamin</first-name>" & _
" <last-name>Franklin</last-name>" & _
" </author>" & _
" <price>8.99</price>" & _
" </book>" & _
"</bookstore>")
รหัส c#
StringReader stream;
stream = new StringReader("<?xml version='1.0'?>" +
"<!-- This file represents a fragment of a book store inventory database. -->" +
"<bookstore>" +
" <book genre=\"autobiography\" publicationdate=\"1981\" ISBN=\"1-861003-11-0\">" +
" <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 genre=\"novel\" publicationdate=\"1967\" ISBN=\"0-201-63361-2\">" +
" <title>The Confidence Man</title>" +
" <author>" +
" <first-name>Herman</first-name>" +
" <last-name>Melville</last-name>" +
" </author>" +
" <price>11.99</price>" +
" </book>" +
" <book genre=\"philosophy\" publicationdate=\"1991\" ISBN=\"1-861001-57-6\">" +
" <title>The Gorgias</title>" +
" <author>" +
" <name>Plato</name>" +
" </author>" +
" <price>9.99</price>" +
" </book>" +
"</bookstore>");
- ตัวอย่างรหัสที่ใช้ในการStreamReaderวัตถุ:
กระบวนการStreamReaderวัตถุถูกใช้ในการอ่านอักขระจากแฟ้ม อ่านในชื่อไฟล์จะอ่านในระหว่างการดำเนินการ:visual Basic .NET รหัส
Dim stream as System.IO.StreamReader
' Loads the XML data in the file books.xml in a new stream.
stream = New StreamReader ("books.xml")
รหัส c#
System.IO.StreamReader stream = new System.IO.StreamReader ("books.xml");
โปรดสังเกตว่า แฟ้ม Books.xml ถูกใช้ต่อไปนี้ คุณสามารถสร้างแฟ้มของคุณเอง Books.xml แฟ้ม Books.xml ตัวอย่างจะยังมีการจัดส่ง โดยใช้ Visual Studio .Net และใน.Net Framework SDK
- สร้างข้อXmlTextReaderคลาสที่ มีกระแสข้อมูล โดยทั่วไปXmlTextReaderใช้ถ้าคุณต้องการเข้าถึง XML เป็นข้อมูลดิบโดยไม่มีค่าผลิตของการเอกสารวัตถุแบบจำลอง (DOM); ดังนั้นXmlTextReaderแสดงกลไกที่เร็วขึ้นสำหรับการอ่าน XMLXmlTextReaderมี constructors ต่าง ๆ เพื่อระบุตำแหน่งที่ตั้งของ XML ข้อมูล รหัสต่อไปนี้โหลดXmlTextReaderจากกระแสข้อมูล:
visual Basic .NET รหัส
Dim reader As XmlTextReader = New XmlTextReader (stream)
รหัส c#
XmlTextReader reader = null;
reader = new XmlTextReader (stream);
- อ่านผ่าน XML โหลดครั้งXmlTextReaderดำเนินการอ่านตามลำดับเพื่อย้ายไปยังข้อมูล XML และใช้การReadวิธีการเรียกใช้ระเบียนถัดไป คุณกลับเท็จถ้าไม่มีเร็กคอร์ไม่มี
visual Basic .NET รหัส
Do While (reader.Read())
' Do some work here on the data.
Console.WriteLine(reader.Name)
Loop
' Reading of the XML file has finished.
Console.ReadLine() 'Pause
รหัส c#
while (reader.Read())
{
// Do some work here on the data.
...
}
while (reader.Read())
{
// Do some work here on the data.
Console.WriteLine(reader.Name);
}
Console.ReadLine();
- ตรวจสอบโหนด การประมวลผลข้อมูล XML แต่ละระเบียนมีชนิดเป็นโหน ซึ่งสามารถถูกกำหนดจากNodeTypeคุณสมบัติ กระบวนการชื่อ:และค่าproperties return the node name (the element and attribute names)
and the node value (the node text) of the current node (or record). กระบวนการNodeTypeระบุเป็นตัวกำหนดชนิดของโหนด The following example
displays the name of the elements and the document type. Note that this example
ignores element attributes.
visual Basic .NET รหัส
Do While (reader.Read())
Select Case reader.NodeType
Case XmlNodeType.Element 'Display beginning of element.
Console.Write("<" + reader.Name)
Console.WriteLine(">")
Case XmlNodeType.Text 'Display the text in each element.
Console.WriteLine(reader.Value)
Case XmlNodeType.EndElement 'Display end of element.
Console.Write("</" + reader.Name)
Console.WriteLine(">")
End Select
Loop
รหัส c#
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 end of element.
Console.Write("</" + reader.Name);
Console.WriteLine(">");
break;
}
}
- ตรวจสอบแอตทริบิวต์ Element node types can include a
list of attribute nodes that are associated with them. กระบวนการMovetoNextAttributeวิธีย้ายถึงแต่ละแอตทริบิวต์ในองค์ประกอบตามลำดับ
ใช้แบบHasAttributesคุณสมบัติการทดสอบว่าโหนดที่มีแอตทริบิวต์ใด ๆ กระบวนการAttributeCountคุณสมบัติส่งกลับจำนวนของแอตทริบิวต์สำหรับโหนดปัจจุบัน
visual Basic .NET รหัส
Do While (reader.Read())
Select Case reader.NodeType
Case XmlNodeType.Element 'Display beginning of element.
Console.Write("<" + reader.Name)
If reader.HasAttributes Then 'If attributes exist
While reader.MoveToNextAttribute()
'Display attribute name and value.
Console.Write(" {0}='{1}'", reader.Name, reader.Value)
End While
End If
Console.WriteLine(">")
Case XmlNodeType.Text 'Display the text in each element.
Console.WriteLine(reader.Value)
Case XmlNodeType.EndElement 'Display end of element.
Console.Write("</" + reader.Name)
Console.WriteLine(">")
End Select
Loop
รหัส c#
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an Element.
Console.Write("<" + reader.Name);
while (reader.MoveToNextAttribute()) // Read attributes.
Console.Write(" " + reader.Name + "='" + reader.Value + "'");
Console.Write(">");
Console.WriteLine(">");
break;
case XmlNodeType.Text: //Display the text in each element.
Console.WriteLine (reader.Value);
break;
case XmlNodeType. EndElement: //Display end of element.
Console.Write("</" + reader.Name);
Console.WriteLine(">");
break;
}
}
- Completed code is provided here for your convenience.
Visual Basic.Net code
Imports System.Xml
Imports System.IO
Module Module1
Sub Main()
Dim stream As System.IO.StreamReader
' Loads the XML data in the file books.xml in a new stream.
stream = New StreamReader("books.xml")
Dim reader As XmlTextReader = New XmlTextReader(stream)
Do While (reader.Read())
Select Case reader.NodeType
Case XmlNodeType.Element 'Display beginning of element.
Console.Write("<" + reader.Name)
If reader.HasAttributes Then 'If attributes exist
While reader.MoveToNextAttribute()
'Display attribute name and value.
Console.Write(" {0}='{1}'", reader.Name, reader.Value)
End While
End If
Console.WriteLine(">")
Case XmlNodeType.Text 'Display the text in each element.
Console.WriteLine(reader.Value)
Case XmlNodeType.EndElement 'Display end of element.
Console.Write("</" + reader.Name)
Console.WriteLine(">")
End Select
Loop
End Sub
End Module
รหัส c#
using System;
using System.Xml;
using System.IO;
namespace ReadXMLFromStream
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static void Main(string[] args)
{
System.IO.StreamReader stream = new System.IO.StreamReader ("books.xml");
XmlTextReader reader = null;
reader = new XmlTextReader (stream);
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an Element.
Console.Write("<" + reader.Name);
while (reader.MoveToNextAttribute()) // Read attributes.
Console.Write(" " + reader.Name + "='" + reader.Value + "'");
Console.Write(">");
Console.WriteLine(">");
break;
case XmlNodeType.Text: //Display the text in each element.
Console.WriteLine (reader.Value);
break;
case XmlNodeType. EndElement: //Display end of element.
Console.Write("</" + reader.Name);
Console.WriteLine(">");
break;
}
}
}
}
}
- Build and Run your project.
For more information, see the "XML in Microsoft .NET: .NET
Framework XML Classes and C# Offer Simple, Scalable Data Manipulation" article
from
msdn Magazineat the following Microsoft Web site:
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการ
XmlReader,
StreamReaderและ
StringReaderclasses, see the Microsoft .NET Framework Class Library
documentation.
For more information about using
XmlReaderto read XML data, see the Microsoft .NET Framework Developer's
Guide documentation.
หมายเลขบทความ (Article ID): 301228 - รีวิวครั้งสุดท้าย: 10 มกราคม 2554 - Revision: 4.0
ใช้กับ
- Microsoft .NET Framework 1.1
| kbhowtomaster kbmt KB301228 KbMtth |
แปลโดยคอมพิวเตอร์ข้อมูลสำคัญ: บทความนี้แปลโดยซอฟต์แวร์การแปลด้วยคอมพิวเตอร์ของ Microsoft แทนที่จะเป็นนักแปลที่เป็นบุคคล Microsoft มีบทความที่แปลโดยนักแปลและบทความที่แปลด้วยคอมพิวเตอร์ เพื่อให้คุณสามารถเข้าถึงบทความทั้งหมดในฐานความรู้ของเรา ในภาษาของคุณเอง อย่างไรก็ตาม บทความที่แปลด้วยคอมพิวเตอร์นั้นอาจมีข้อบกพร่อง โดยอาจมีข้อผิดพลาดในคำศัพท์ รูปแบบการใช้ภาษาและไวยากรณ์ เช่นเดียวกับกรณีที่ชาวต่างชาติพูดผิดเมื่อพูดภาษาของคุณ Microsoft ไม่มีส่วนรับผิดชอบต่อความคลาดเคลื่อน ความผิดพลาดหรือความเสียหายที่เกิดจากการแปลเนื้อหาผิดพลาด หรือการใช้บทแปลของลูกค้า และ Microsoft มีการปรับปรุงซอฟต์แวร์การแปลด้วยคอมพิวเตอร์อยู่เป็นประจำ
ต่อไปนี้เป็นฉบับภาษาอังกฤษของบทความนี้:
301228
(http://support.microsoft.com/kb/301228/en-us/
)