Lm th? no ? ?c d? li?u XML t? m?t d?ng su?i trong.NET Framework SDK

D?ch tiu ? D?ch tiu ?
ID c?a bi: 301228 - Xem s?n ph?m m bi ny p d?ng vo.
Bung t?t c? | Thu g?n t?t c?

? Trang ny

TM T?T

Bi vi?t ny ch?ng t? lm th? no ? s? d?ng cc XmlTextReader l?p ?c Extensible Markup Language (XML) t? m?t d?ng su?i. Cc d?ng c th? ?n t? nhi?u ngu?n, ch?ng h?n nh m?t d?ng byte t? m?t my ch?, m?t t?p tin, ho?c TextReader l?p h?c.

Yu c?u

Danh sch sau v?ch ra ?c ? ngh? ph?n c?ng, ph?n m?m, h? t?ng m?ng, v gi d?ch v? c?n thi?t:
  • Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, ho?c my ch? Windows NT 4.0
  • Microsoft Visual Studio.NET
Bi vi?t ny gi? ?nh r?ng b?n ? quen thu?c v?i sau cc ch? ?:
  • Thu?t ng? XML
  • Vi?c t?o v ?c XML

Lm th? no ? ?c d? li?u XML t? m?t d?ng su?i

  1. M? ph?ng thu tr?c quan.NET.
  2. T?o m?t Microsoft Visual Basic (VB) m?i ho?c Microsoft Visual C# giao di?n i?u khi?n ?ng d?ng.

    LU ?: Cc b?c sau y cung c?p m?t m t? chi ti?t lm th? no ? xy d?ng cc ?ng d?ng. B?n c?ng c th? i tr?c ti?p ?n b?c 10, trong tr?ng h?p hon thnh m? ?c cung c?p.
  3. ?m b?o r?ng cc d? n c ch?a m?t tham chi?u ?n cc System.xml v System.io khng gian tn.
  4. S? d?ng cc bo co hng nh?p kh?u vo cc XML khng gian tn do b?n khng ph?i v?t qua v?ng lo?i XmlTextReader khai bo trong khng gian tn sau ny trong m? c?a b?n. B?n ph?i s? d?ng tuyn b? nh?p kh?u tr?c ? b?t k? t? khai khc nh sau:
    Visual Basic.NET m?
    Imports System.Xml
    Imports System.IO
    						
    Visual C# M?
    using System.Xml;
    using System.IO;
    						
  5. T?o ho?c truy xu?t cc d?ng XML. M?t d?ng su?i l m?t tr?u t?ng ?i di?n c?a m?t thi?t b? ?u vo ho?c ?u ra l ngu?n g?c c?a, ho?c i?m ?n cho, d? li?u (trong tr?ng h?p, d? li?u XML ny). B?n c th? vi?t v ?c t? m?t d?ng su?i, m t?t nh?t ?c h?nh dung nh l m?t d?ng ch?y c?a byte.

    Su?i l ?c s? d?ng ? cung c?p ?c l?p v?i thi?t b? v do ?i h?i ph?i c chng tr?nh thay ?i n?u, v d?, l ngu?n g?c c?a m?t d?ng thay ?i. C m?t vi cch khc nhau ? t?o ra m?t d?ng cho cc XmlTextReader l?p h?c. Ch?n m?t trong cc m?u m? sau y ? thm vo cc Th? t?c chnh c?a module m?c ?nh:
    • M?u m? c s? d?ng cc StringReader ?i t?ng:

      Cc StringReader ?i t?ng ?c k? t? t? dy v m?t trong m?t chu?i gi tr? khi xy d?ng.Visual Basic.NET Code
      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# Code
      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>");
      						
    • M?u m? c s? d?ng cc StreamReader ?i t?ng:

      Cc StreamReader ?i t?ng ?c s? d?ng ? ?c k? t? t? t?p tin. N ?c trong cc tn t?p ?c ?c trong th?i gian xy d?ng:Visual Basic.NET Code
      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# Code
      System.IO.StreamReader stream = new System.IO.StreamReader ("books.xml");
      						
      Lu ? r?ng cc t?p tin Books.xml ?c s? d?ng ? y. B?n c th? t?o ring c?a b?n Books.XML t?p tin. M?t m?u Books.xml file c?ng ?c v?n chuy?n v?i Visual Studio.M?ng l?i v cc.NET Framework SDK.

  6. Xy d?ng m?t XmlTextReader l?p h?c v?i cc d?ng. Thng th?ng, XmlTextReader ?c s? d?ng n?u b?n c?n truy c?p cc XML nh d? li?u th m khng c cc chi ph c?a m?t Document Object Model (DOM); V? v?y, XmlTextReader cung c?p m?t c ch? nhanh hn ? ?c XML. XmlTextReader c nh th?u khc nhau ? xc ?nh v? tr cc XML d? li?u. Cc m? sau y t?i XmlTextReader t? m?t d?ng:
    Visual Basic.NET Code
    Dim reader As XmlTextReader = New XmlTextReader (stream)
    						
    C# Code
    XmlTextReader reader = null;	
    reader = new XmlTextReader (stream);
    						
  7. ?c qua cc XML. M?t l?n t?i, XmlTextReader th?c hi?n tu?n t? l?n ?c ? di chuy?n qua cc d? li?u XML v s? d?ng cc oc phng php ? c ?c cc b?n ghi ti?p theo. N tr? v? False n?u khng c khng c h? s b?.
    Visual Basic.NET Code
    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# Code
    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();
    						
  8. Ki?m tra cc nt. X? l? d? li?u XML, m?i b?n ghi c m?t lo?i nt c th? ?c xc ?nh t? cc NodeType b?t ?ng s?n. Cc Tn v Gia tri ti s?n tr? l?i nt tn (cc nguyn t? v thu?c tnh tn) v gi tr? nt (vn b?n n?t) hi?n t?i nt (ho?c h? s). Cc NodeType li?t k s? xc ?nh lo?i nt. V d? sau hi?n th? tn c?a cc y?u t? v lo?i ti li?u. Lu ? r?ng v d? ny b? qua cc thu?c tnh nguyn t?.
    Visual Basic.NET Code
    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# Code
    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;
        }
    }
    						
  9. Ki?m tra cc thu?c tnh. Lo?i n?t nguyn t? c th? bao g?m m?t danh sch cc thu?c tnh nt ?c lin k?t v?i h?. Cc MovetoNextAttribute phng php di chuy?n theo tu?n t? qua m?i thu?c tnh nguyn t?. S? d?ng cc HasAttributes b?t ?ng s?n ? th? nghi?m cho d nt c thu?c tnh. Cc AttributeCount b?t ?ng s?n tr? v? s? l?ng cc thu?c tnh cho nt hi?n t?i.
    Visual Basic.NET Code
    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# Code
    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;
        }
    }
    						

  10. Hon thnh m? ?c cung c?p ? y thu?n ti?n cho b?n.
    Visual Basic.Net m?
    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# Code
    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;
    				}
    			}
    
    		}
    	}
    }
    
    						
  11. Xy d?ng v ch?y d? n c?a b?n.

THAM KH?O

? bi?t thm chi ti?t, xem XML"trong Microsoft.NET:.NET Bi vi?t cc l?p XML khun kh? v C# cung c?p Simple, kh? nng m? r?ng d? li?u thao tc" t? MSDN t?p ch t?i Microsoft Web site sau:
http://MSDN.Microsoft.com/msdnmag/issues/01/01/XML/Default.aspx
? bi?t thm thng tin v? cc XmlReader, StreamReader, v StringReader cc l?p h?c, xem Microsoft.Th vi?n l?p NET Framework ti li?u h?ng d?n.

? bi?t thm thng tin v? vi?c s? d?ng XmlReader ? ?c d? li?u XML, xem Microsoft.NET Framework nh pht tri?n Ti li?u h?ng d?n.

Thu?c tnh

ID c?a bi: 301228 - L?n xem xt sau cng: 26 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft .NET Framework 1.1
T? kha:
kbhowtomaster kbmt KB301228 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:301228

Cung cp Phan hi

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com