Visual C# ?? ????? ???? ???? ????? ?? XML ???? ?????

???? ?????? ???? ??????
???? ID: 307548 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

?? ???? ???? ????? ?? ??????????? ??????? ???????? (XML) ????? ?? ??? XmlTextReader ????? ?? ????? ???? ?? ????? ?? ????? ???? ??. XmlTextReader XML ?? ????????? ???????? ?? ??????????? ?????? ???? ?? ?? XML 1.0 ??????????? ?? ??? ?? World Wide Web Consortium (W3C) ?? XML ????????? ??? ?????????? ?? ??????????? ???? ??. ?? ???? XML ????????? ???????? ???? (DOM) ???? ???????? ???? ?? ?????? ???? ?? ???? XML ?? ???, ?????????? ??????? ????? ?????? ???? ??.

??????????

????? ???? ???? ??? ?????? ???????? ?????????, ??????????, ??????? ??????, ?? ?????? ????? ?? ?????????? ???? ??.
  • Microsoft Visual Studio 2005 ?? Microsoft Visual Studio .NET
?? ???? ????? ?? ?? ?? ????? ?????? ?? ?????? ???:
  • XML ????????
  • XML ????? ????? ?? ?????

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

?? ?????? ??? Books.xml ???? ????? ?? ?????? ???? ??? ??. ?? ???? Books.xml ????? ??? ???? ??? ?? ????? ??????? ??? .NET ?????????? ????????? ??? (SDK) ?????????????? ?? ??? ????? ????? ????? ?? ?????? ?? ???? ???.
\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Samples\QuickStart\Howto\Samples\Xml\Transformxml\Cs
???? Books.xml ?? \Bin\Debug ??????? ??? ????????? ????? ????, ?? ?? ??????? ??? ????? ?? ?????? ???? ?? ????????? ????? ??. Books.xml ??????? ?? ??? ?? ?????? ??. ??????? ????? ?? ??? "??????" ?????? ?????.
  1. Visual Studio 2005 ?? Visual Studio .NET ??????? ????.
  2. ?? ??? Visual C# ????? ????????? ?????. ???? "????? ??? ???????" ?????? ??? ???? ?? ????????? ????? ?? ??? ?? ????? ?? ?????? ?? ??? ??????
  3. ????????? ???? ?? ????????? ??? System.Xml.dll ??????? ?? ?????? ??.
  4. System.Xml ???????? ?? ????????? ?? ????? ???? ????????? ????, ???? ???? ??? ??? ???? ??? ??? XmlTextReader ??????? ?? ?????? ??????? ???? ?? ???????? ???? ??. ???? ???? ????? ?? ????, ???? ????????? ?? ????? ???? ?? ?????? ???? ?????.
    System.Xml ?? ????? ????;
    					
  5. ?? XmlTextReader ???????? ?? ??????? ?????, ?? ??? XML ????? ?? ??? ????????? ????. ????? ??? ??, ??? ?? XML ?? ?? DOM ?? ?????? ?? ???? ????????? ???? ?? ??? ??? ????? ????? ???, ?? XmlTextReader ????? ?? ????? ???? ???? ??; ?? ??????, XmlTextReader ????? XML ????? ?? ??? ?? ??????? ??????? ?????? ???? ??. XML ???? ?? ????? ????????? ???? ?? ??? XmlTextReader ????? ??? ??????? ??????????? ???. ????? ??? XmlTextReader ????? ?? ??????? ????? ?? ?? Books.xml ????? ??? ???? ??. ????? ??? ?? Class1 ?? ????? ???????? ??? ??????.
    XmlTextReader reader = new XmlTextReader ("books.xml");
    					
  6. XML ?? ?????? ?? ?????. (????? ??? ?? ????? ??? ?? ????? "while" ??? ?? ??????? ??, ?? ???? ?? ??? XML ?? ????? ?? ??? ?? ??? ?? ????? ???? ?? ????? ??????? ???.) XML ???? ????? ?? ??? XmlTextReader ???????? ????? ?? ??? Read ?????? ?? ????? ????. Read ?????? XML ????? ??? ?????? ??? ?? ?? ?? ???? ???? ??, ?? ?? ?? ?? ???? ?? ??? ??? ???? ????? ????, ???? Read ?????? "False" ??? ?????? ??.
    while (reader.Read()) 
    {
        // ???? ???? ?? ??? ????? ????.
    	Console.WriteLine(reader.Name);
    }
    Console.ReadLine();
    					
  7. ????? ?? ???????? ????. XML ???? ??????? ???? ?? ???, ???????? ??????? ??? ?? ??? ?????? ???? ?? ???? NodeType ??? ?????? ????????? ???? ?? ???? ??. Name ?? Value ??? ??? ?? ??? (???? ?? ??????? ?? ???) ?? ??????? ??? (?? ???????) ?? ??? ??? (??? ???) ?????? ???. NodeType ???? ??? ?? ?????? ?? ???????? ???? ??. ????? ????? ??? ?????? ?? ????????? ?? ?????? ?? ????????? ???? ??. ????? ??? ?? ?? ????? ???? ????????? ?? ?????? ???? ??.
    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 the end of the element.
                Console.Write("</" + reader.Name);
       Console.WriteLine(">");
                break;
        }
    }
    					
  8. ????????? ?? ???????? ????. ???? ??? ?????? ??? ??????? ??????? ????? ?? ???? ????? ?? ???? ??. MovetoNextAttribute ?????? ???? ??? ???????? ??????? ??? ?????? ??? ?? ???? ??. ?? ?????? ?? ??? HasAttributes ??? ?? ????? ???? ?? ??? ??? ??? ????????? ??? ?? ????. AttributeCount ??? ??????? ??? ?? ??? ????????? ?? ?????? ?????? ??.
    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.WriteLine(">");
                   break;
         case XmlNodeType.Text: //Display the text in each element.
                   Console.WriteLine (reader.Value);
                   break;
         case XmlNodeType. EndElement: //Display the end of the element.
                   Console.Write("</" + reader.Name);
          Console.WriteLine(">");
                   break;
           }
       }
    					
  9. ???? ????????? ?????? ?? ??? ????.

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

using System;
System.Xml ?? ????? ????;

namespace ReadXMLfromFile
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
        static void Main(string[] args)
        {
            XmlTextReader reader = new XmlTextReader ("books.xml");
            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 the end of the element.
                        Console.Write("</" + reader.Name);
                        Console.WriteLine(">");
                        break;
                }
            }
            Console.ReadLine();
        }
    }
}
				

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

<bookstore>
<book>
<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>
<title>
The Confidence Man
</title>
<author>
<first-name>
Herman
</first-name>
<last-name>
Melville
</last-name>
</author>
<price>
11.99
</price>
</book>
<book>
<title>
The Gorgias
</title>
<author>
<name>
Plato
</name>
</author>
<price>
9.99
</price>
</book>
</bookstore>
				

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

?? ?? ??? ?? ???? ???? ???, ?? ???? ????? ????? ?????? ????? ??????? ?? ???? ??: ????? ???? ???? ??? ?????: System.Xml.XmlException: ????????? XML ?????. XML ????? ????????? ??? ???? ??? ???? ?????, ?? ???? ???? ????? ?????? ?????? ?? ????? ???? ?? ?????? ???? ??.

???????? ???????? ?????? ??????. ????? ?????? ??? ?? ????? ?????? ?? ???? ??? While
(reader.Read())
????? ?????? ?? ?????? ????????? ??????? ?? ???? ??????? ???? ??. ?????? ?? ???, ????????? ??????? ??? ??????? ????????? ?? ???? ???. ????? ?? ?????? ????????? ??????? ?? ?????? ???
<?xml version='1.0' ?>
?? xml ??? ??? ?<? ?????? ?? ???? ?? ?????? ??. ?????? ?? ??? ???? ?? ??? ?????????? ????? ?????? ?? ?????.

??????

????? ????? Microsoft ??????? ?????? ?? ??????? ?? ??? ?????? ??:
?? ??? ?? ??????? ?????? ??? ?? ??????? ????
??????? ????
Books.exe ????? ??? ??????? ????.
.NET Framework XML ???????? ?? C# ?? ???? ??? ???? ??????? ?? ???, ????? Microsoft Developer Network (MSDN) ??? ???? ?? ????:
http://msdn.microsoft.com/en-us/magazine/cc302158.aspx
XmlReader ????? ?? ???? ??? ???? ??????? ?? ???, ????? MSDN ??? ???? ?? ????:
http://msdn2.microsoft.com/en-us/library/system.xml.xmlreader(vs.71).aspx
XML ???? ????? ?? ??? XmlReader ?? ????? ???? ?? ????? ?? ???? ??? ???? ??????? ?? ???, ????? MSDN ??? ???? ?? ????:
http://msdn2.microsoft.com/en-us/library/aa720470(VS.71).aspx

http://msdn2.microsoft.com/en-us/library/tfz3cz6w(vs.71).aspx

???

???? ID: 307548 - ????? ???????: 30 ????? 2013 - ??????: 6.0
???? ???? ???? ??:
  • Microsoft Visual C# 2008 Express Edition
  • Microsoft Visual C# 2005
  • Microsoft Visual C# .NET 2002 Standard Edition
??????: 
kbdownload kbhowtomaster KB307548

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

 

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