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

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

?? ??? ??????

??????

???? ??? ??????? ????? ??????? ??? XmlTextReader ?????? ??? ??????? ??????? (XML) ?? ???. ???? ?? ???? ????? ?? ?????? ?????? ?? ????? ??? ??? ???? ?? ???? ??? ?? ??? TextReader.

???????

???? ??????? ??????? ??????? ?????? ??? ????? ?????? ???????? ?????? ? ??? ??????? ???:
  • Microsoft Windows 2000 Professional, Windows 2000 Server 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. ?????? ????? ????????? ??? ????? ??? Xml ???? ??? ?????? ????? ??????? XmlTextReader ?? ????? ??? ??? ???? ??? ?? ????????? ???????? ?????? ??. ??? ???? ??????? ??????? ??????? ??? ?? ????????? ?????? ??? ???:
    ????????? ???????? Visual Basic .NET
    Imports System.Xml
    Imports System.IO
    						
    Visual C# code
    using System.Xml;
    using System.IO;
    						
  5. ????? ?? ??????? ??? XML. ??? ?? ????? ????? ?? ????? ?? ????? ?????? ???? ??? ?????? ?? ???? ?? ???????? (?? ??? ??????? ?????? XML). ???? ??????? ??? ? ??????? ?? ??? ???? ??? visualized ??? ???? ????.

    ??? ??????? ?????? ???? ??????? ?? ?????? ????? ????? ?? ??????? ???????? ??? ??? ???? ??????? ????? ???? ???. ???? ??? ??? ?????? ?????? ??? ???? 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 ?? .Net Visual Studio ? .Net Framework SDK.

  6. ????? ???? ???? XmlTextReader ?? ?????. ???? ???? ??? ??????? XmlTextReader ??? ??? ????? ?????? ??? XML ??? ???????? ????? ???? ?????? ?? ???? ???? ??????? "(DOM); ????? XmlTextReader ???? ???? ???? ????? XML. ??? XmlTextReader ????????? ?????? ?????? ???? XML ????????. ????? ???????? ???????? ??????? XmlTextReader ?? ???:
    ??? .NET Visual Basic
    Dim reader As XmlTextReader = New XmlTextReader (stream)
    						
    C# Code
    XmlTextReader reader = null;	
    reader = new XmlTextReader (stream);
    						
  7. ????? XML. ????? ?????, ???? ??????? ?????? ???? ??? ?????? XML XmlTextReader ?? ?????? ??????? ????? ?????? ??? ????? ??????. ???? ?????? ??? ??? ???? ???? ????? ??.
    ??? .NET Visual Basic
    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. ??? ?????. ??????? ?????? XML ????? ?? ??? ??? ??????? ???? ????? ?? ??????? NodeType. ????? ??????? Name ? ?????? ??? ?????? (????? ???? ? ???) ? ???? ???? (?? ????) ?????? ??????? (?? ???). ????? NodeType ???? ??? ????. ???? ?????? ?????? ??? ??????? "?" ??? ???????. ???? ?? ??? ?????? ?????? ???? ????.
    ??? .NET Visual Basic
    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. ??? ??????. ???? ?? ????? ????? ???? ???? ????? ????? ????? ???????? ???. ??? ????? MovetoNextAttribute ???????? ???? ??? ??? ?? ??????. ?????? ??????? HasAttributes ??????? ?? ??? ??? ?? ???? ??????. ????? ????? AttributeCount ??? ???? ?????? ???????.
    ??? .NET Visual Basic
    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. ??? ????? ??? ???????? ??? ?????.
    ??? 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. ????? ?? ????? ???????.

?????

????? ?? ?????????? ???? "XML ?? Microsoft .NET: .NET Framework ???? XML ? ???? C# ???? ?????? ?????? ????? ???????" ??????? ?? Magazine MSDN ?? ???? Microsoft ?????? ??? ?????:
http://msdn.microsoft.com/msdnmag/issues/01/01/xml/default.aspx
????? ?? ????????? ??? XmlReaderStreamReader ????? StringReader "????? ?????? ????? ??" Microsoft .NET Framework ???? ???????.

????? ?? ????????? ??? ??????? XmlReader ????? ?????? XML ???? Microsoft .NET Framework ????? "???? ???????.

???????

???? ???????: 301228 - ????? ??? ??????: 27/????? ??????/1425 - ??????: 3.4
????? ???
  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework Class Libraries 1.1
????? ??????: 
kbmt kbhowtomaster KB301228 KbMtar
????? ????
???: ??? ????? ??? ?????? ???????? ?????? ????? ???? ????? ?????????? ????? ?? ????????? ?????? ????. ???? ???? ?????????? ???? ?? ???????? ???????? ?????? ????????? ????? ????????? ???????? ????? ???????? ?????? ?? ?????? ??? ?? ???????? ???????? ?? ????? ??????? ?????? ??? ??????? ?????? ??. ?????? ?? ???? ??? ??????? ???????? ????? ?? ???? ????? ?????? ??? ????? ??? ????? ??????? ?? ????? ?? ?????? ??? ??? ??????? ??????? ?? ????? ????? ????? ????? ?????. ?? ????? ???? ?????????? ??????? ??? ????? ?? ??????? ?? ????? ?????? ?? ??? ????? ?? ????? ??????? ?? ???????? ?? ??? ???????. ???? ???? ?????????? ???????? ??? ????? ?????? ??????? ??????
???? ??? ????? ??????? ?????? ??????????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