Lm th? no ? ?c XML t? m?t t?p tin b?ng cch s? d?ng Visual C#

D?ch tiu ? D?ch tiu ?
ID c?a bi: 307548 - 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 m t? lm th? no ? s? d?ng l?p XmlTextReader ? ?c m? r?ng nh d?u ki?m ngn ng? (XML) t? m?t t?p tin. XmlTextReader cung c?p phn tch tr?c ti?p v tokenizing c?a XML v th?c hi?n XML 1.0 ?c i?m k? thu?t c?ng nh khng gian tn trong ?c t? XML t? World Wide Web Consortium (W3C). Bi vi?t ny cung c?p truy c?p nhanh, tokenized stream XML thay v? s? d?ng m?t m h?nh ?i t?ng ch?ng h?n nh ti li?u XML Object Model (DOM).

Yu c?u

Danh sch sau phc th?o ?c ? ngh? ph?n c?ng, ph?n m?m, c s? h? t?ng m?ng v gi b?n ghi d?ch v? m b?n c?n:
  • Microsoft Visual Studio 2005 ho?c Microsoft Visual Studio .NET
Bi vi?t ny gi? ?nh r?ng b?n ang quen thu?c v?i cc ch? ? sau:
  • Thu?t ng? XML
  • T?o v ?c m?t t?p tin XML

Lm th? no ? ?c XML t? m?t t?p tin

V d? ny s? d?ng m?t t?p tin tn Books.xml. B?n c th? t?o t?p tin Books.xml c?a ring b?n ho?c dng t?p tin m?u ?c bao g?m v?i QuickStart .NET Kit pht tri?n ph?n m?m (SDK) trong m?c tin th thoai sau y:
\Program Files\Microsoft visual Studio .NET\FrameworkSDK\Samples\QuickStart\Howto\Samples\Xml\Transformxml\Cs
B?n ph?i sao chp Books.xml vo m?c tin th thoai \Bin\Debug, n?m d?i m?c tin th thoai trong b?n t?o ra d? n ny. Books.xml c?ng c s?n ? t?i v?. Xem cc "Ti li?u tham kh?o"ph?n cho v? tr t?i v?.
  1. B?t ?u Visual Studio 2005 ho?c Visual Studio net.
  2. T?o ra m?t m?i Visual C# giao di?n i?u khi?n n. B?n ti?n hnh tr?c ti?p ?n cc "Danh sch m? hon ch?nh"ph?n ho?c ti?p t?c thng qua cc b?c sau ? xy d?ng cc ?ng d?ng.
  3. ?m b?o r?ng cc d? n c ch?a m?t tham chi?u ?n h?i ?ng System.Xml.dll .
  4. Xc ?nh b?ng cch s? d?ng ch? th? trn khng gian tn System.Xml do b?n khng c?n ph?i v?t qua v?ng lo?i XmlTextReader tuyn b? sau trong m? c?a b?n. B?n ph?i s? d?ng ch? th? b?ng cch s? d?ng tr?c khi b?t k? khai bo khc.
    using System.Xml;
    					
  5. T?o m?t th? hi?n c?a ?i t?ng XmlTextReader m?t, v i?n n v?i cc t?p tin XML. Thng th?ng, l?p XmlTextReader ?c s? d?ng n?u b?n mu?n truy c?p vo cc XML nh cc d? li?u th m khng c chi ph c?a m?t DOM; do , cc l?p h?c XmlTextReader cung c?p m?t c ch? nhanh hn cho ?c XML. L?p XmlTextReader c cc nh th?u khc nhau ? xc ?nh v? tr d? li?u XML. M? sau y t?o ra m?t th? hi?n c?a cc l?p h?c XmlTextReader v t?i cc t?p tin Books.xml. Thm m? sau vo th? t?c chnh Class1.
    XmlTextReader reader = new XmlTextReader ("books.xml");
    					
  6. ?c qua cc XML. (Lu ? r?ng b?c ny ch?ng t? m?t bn ngoi "trong khi" v?ng l?p, v ti?p theo hai b?c ch?ng minh lm th? no ? s? d?ng v?ng l?p ? ?c cc XML). Sau khi b?n t?o cc ?i t?ng XmlTextReader , s? d?ng phng php ?c ? ?c d? li?u XML. Phng php ?c ti?p t?c di chuy?n qua cc t?p tin XML tu?n t? cho ?n khi n ?t ?n cu?i c?a t?p tin, lc i?m ?c phng php tr? v? m?t gi tr? c?a "Sai."
    while (reader.Read()) 
    {
        // Do some work here on the data.
    	Console.WriteLine(reader.Name);
    }
    Console.ReadLine();
    					
  7. Ki?m tra cc nt ch?n m?t. x? l d? li?u XML, m?i b?n ghi c m?t lo?i nt ch?n m?t c th? ?c xc ?nh t? b?t ?ng s?n NodeType . Tn v gi tr? ti s?n tr? l?i tn nt ch?n m?t (cc nguyn t? v thu?c tnh tn) v gi tr? nt ch?n m?t (cc vn b?n nt) c?a cc nt ch?n m?t hi?n t?i (ho?c h? s). Li?t k NodeType xc ?nh lo?i nt ch?n m?t. M?u m? sau s? hi?n th? tn c?a cc y?u t? v lo?i ti li?u. Lu ? r?ng m?u ny b? qua thu?c tnh nguyn t?.
    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. Ki?m tra cc thu?c tnh. Lo?i n?t ph?n t? c th? bao g?m m?t danh sch cc thu?c tnh nt ch?n m?t c lin quan v?i h?. Phng php MovetoNextAttribute di chuy?n tu?n t? qua m?i thu?c tnh trong cc y?u t?. S? d?ng cc ti s?n HasAttributes ? ki?m tra xem nt ch?n m?t c thu?c tnh b?t k?. B?t ?ng s?n AttributeCount tr? v? s? l?ng cc thu?c tnh cho cc nt ch?n m?t hi?n t?i.
    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. Lu v ng d? n c?a b?n.

Danh sch m? hon ch?nh

using System;
using 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();
        }
    }
}
				

M?u ?u ra

<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>
				

Gi?i p th?c m?c

Khi b?n ki?m tra m?, b?n c th? nh?n ?c thng bo l?i ngo?i l? sau: khng ?c ngo?i l?: System.Xml.XmlException: khai bo XML b?t ng?. Khai bo XML ph?i l nt ch?n m?t ?u tin trong ti li?u, v khng c k t? ?i di?n kho?ng tr?ng ?c php xu?t hi?n tr?c khi n.

Thng tin b? sung: l?i h? th?ng. Ngo?i l? l?i x?y ra trn d?ng m? sau y: trong khi
(reader.Read())
L?i ngo?i l? gy ra b?i m?t l?nh x? l khng h?p l?. V d?, h?ng d?n x? l? c th? ch?a d?u ki?m khng lin quan. Sau y l m?t v d? v? m?t l?nh x? l khng h?p l?:
<?xml version='1.0' ?>
Th? xml ny c m?t khng gian tr?c cc '< bracket.="" remove="" the="" preceding="" whitespace="" to="" resolve="" the=""></>

TI LI?U THAM KH?O

T?p sau c s?n ? t?i v? t? Microsoft Download Center:
Thu g?n h?nh ?nh nyBung r?ng h?nh ?nh ny
T?i v?
T?i v? gi Books.exe by gi?.
? bi?t thm chi ti?t v? cc l?p h?c Khun kh? .NET XML v C#, gh thm Web site sau c?a Microsoft Developer Network (MSDN):
http://MSDN.Microsoft.com/en-US/Magazine/cc302158.aspx
? bi?t thm chi ti?t v? cc class XmlReader , truy c?p vo MSDN Web site sau:
http://msdn2.Microsoft.com/en-US/Library/System.xml.xmlreader (vs.71).aspx
? bi?t thm chi ti?t v? lm th? no ? s? d?ng XmlReader ? ?c d? li?u XML, h?y truy c?p MSDN Web site sau:
http://msdn2.Microsoft.com/en-US/Library/aa720470 (VS.71).aspx

http://msdn2.Microsoft.com/en-US/Library/tfz3cz6w (vs.71).aspx

Thu?c tnh

ID c?a bi: 307548 - L?n xem xt sau cng: 09 Thang Hai 2014 - Xem xt l?i: 4.0
p d?ng
  • Microsoft Visual C# 2008 Express Edition
  • Microsoft Visual C# 2005
  • Microsoft Visual C# .NET 2002 Standard Edition
T? kha:
kbdownload kbhowtomaster kbmt KB307548 KbMtvi
My d?ch
QUAN TRONG: Bai vit nay c d?ch b?ng phn mm dich thu?t cua Microsoft va co th c Cng ng Microsoft chinh sa l?i thng qua cng ngh CTF thay vi mt bin dich vin chuyn nghip. Microsoft cung c?p cc bi vi?t ?c c? bin d?ch vin v ph?n m?m d?ch thu?t th?c hi?n v c?ng ?ng ch?nh s?a l?i ? 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 nhi?u ngn ng? Tuy nhin, bi vi?t do my d?ch hoc thm chi cng ng chinh sa sau khng ph?i lc no c?ng hon h?o. Cc bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, 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.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny: 307548

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