XML ???? ?? ?? ??????? ??? .NET Framework SDK ?? ????? ???? ????

???? ?????? ???? ??????
???? ID: 301228 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

????????? ???? ?? ?? ???? ?? ?? ????? ???? ????XmlTextReader??????????? ??????? ???? (XML) ?? ?? ??????? ?? ???? ?? ??? ????? ??????? ?? ???? ??? ??? ?????, ???? ??? ????? ?? ?? ???? ??????? ?? ?? ??? ?? ??? ????? ?? ???TextReader?????

??????????

????? ???? ??? ???????? ?????????, ??????????, ??????? ??????, ?? ?????? ??? ?? ???????? ???? ?? outlines ??:
  • Microsoft Windows 2000 Professional ???, Windows 2000 ?????, Windows 2000 ?????, ?? Windows NT 4.0 ????? ????? ????
  • Microsoft Visual Studio .NET
?? ???? ????? ?? ?? ?? ????? ???? ?? ?????? ???:
  • XML ??????
  • ??? ??? ?? ?? XML ????

XML ???? ?? ???? ??????? ?? ????? ???? ????

  1. Visual Studio .NET ??????
  2. ??? ??? Microsoft Visual Basic (VB) ?? Microsoft Visual C# ????? ????????? ??????

    ???:: ????? ????? ?? ???? ????????? ????? ?? ??? ???? ?? ??????? ????? ?????? ????? ?? ?? ???? ??? ?? ???? ??? ?? ???? 10, ???? ????? ??? ???? ???
  3. ????????? ???? ?? ????????? ?? ??? ??? ?????? ???System.Xml, ??System.IO??? ????? ???
  4. ?? IMPORTS ??? ?? ????? ????XML???????? ???? ?? ????? ????? ?? ??? ?????? ???? ??XmlTextReader???? ??? ??? ??? ??? ?? ??? ????? ??? declarations. ?? ?? ??? ??? ???? ?? ???? declarations ???? ?? ???? IMPORTS ??? ?? ????? ???? ?????:
    Visual Basic .NET ???
    Imports System.Xml
    Imports System.IO
    						
    ????? C# ???
    using System.Xml;
    using System.IO;
    						
  5. ????? ?? XML ??????? ?? ??????? ????? ???? ?????? ?? ?? ??? ????? ?????? ?????? ?? ????? ??, ??, ???? ?? ??? (?? ?????? ???, XML ????) ??? ?????? ?? ??? ???? ??? You can write to and read from a stream, which is best visualized as a flow of bytes.

    Streams are used to provide independence from the device and therefore require no program changes if, for example, the source of a stream changes. There are a few different ways to create a stream for theXmlTextReader????? Select one of the following code samples to add to the Main procedure of the default module:
    • Code sample that uses theStringReader????????:

      TheStringReaderobject reads characters from strings and takes in a string value during construction.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# 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>");
      						
    • Code sample that uses theStreamReader????????:

      TheStreamReaderobject is used to read characters from files. It reads in the file name to be read during construction: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# Code
      System.IO.StreamReader stream = new System.IO.StreamReader ("books.xml");
      						
      Note that the Books.xml file is used here. You can create your own Books.xml file. A sample Books.xml file is also shipped with Visual Studio .Net and the .Net Framework SDK.

  6. Construct anXmlTextReaderclass with the stream. ??????????,XmlTextReaderis used if you need to access the XML as raw data without the overhead of a Document Object Model (DOM); therefore,XmlTextReaderprovides a faster mechanism for reading XML.XmlTextReaderhas different constructors to specify the location of the XML data. The following code loadsXmlTextReaderfrom a stream:
    Visual Basic .NET ???
    Dim reader As XmlTextReader = New XmlTextReader (stream)
    						
    C# Code
    XmlTextReader reader = null;	
    reader = new XmlTextReader (stream);
    						
  7. Read through the XML. Once loaded,XmlTextReaderperforms sequential reads to move across the XML data and uses the????method to get the next record. It returns False if there are no more records.
    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# 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. Inspect the nodes. To process the XML data, each record has a node type, which can be determined from theNodeType???? The???, ??Value:properties return the node name (the element and attribute names) and the node value (the node text) of the current node (or record). TheNodeTypeenumeration determines the node type. 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# 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. Inspect the attributes. Element node types can include a list of attribute nodes that are associated with them. TheMovetoNextAttributemethod moves sequentially through each attribute in the element. ?????HasAttributesproperty to test whether the node has any attributes. TheAttributeCount??? ??????? ??? ?? ??? ????????? ?? ?????? ???? ???
    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;
        }
    }
    						

  10. ????? ??? ???? convenience ?? ??? ???? ?????? ???? ?? ???
    ????? Basic.Net ???
    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;
    				}
    			}
    
    		}
    	}
    }
    
    						
  11. ?????, ?? ???? ????????? ?? ??????

??????

???? ??????? ?? ??? ????? "Microsoft .NET ??? XML: .NET Framework XML ???? ?? C# ???????? ??????, ???????? ???? Manipulation" ?? ????MSDN Magazine????? Microsoft ??? ???? ??:
HTTP://MSDN.Microsoft.com/msdnmag/issues/01/01/XML/default.aspx
?? ???? ??? ???? ??????? ?? ???XmlReader,StreamReader, ??StringReader???? ?? ???, ????? Microsoft .NET Framework ????? ????????? ???????? ??????

????? ?? ???? ??? ???? ??????? ?? ???XmlReaderXML ???? ?? ???? ?? ??? Microsoft .NET Framework ?????? ?? ????? ???????? ?????????????

???

???? ID: 301228 - ????? ???????: 04 ?????? 2010 - ??????: 5.0
???? ???? ???? ??:
  • Microsoft .NET Framework 1.1
??????: 
kbhowtomaster kbmt KB301228 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:301228

??????????? ???

 

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