Xmltextreader is a stream-based XML reader similar to sax. Unlike sax, Sax adopts the PUSH model push mode and xmltextreader adopts the PULL model.ProgramDevelopers can process data more selectively. The following is a simple example:
Code
1 String Xmlstr = " <? XML version = \ " 1.0 \ " Encoding = \ " UTF - 8 \ " ?> <Books> <book Sn = \ " 1000 \ " Name = \ " . Net \ " /> <Book Sn = \ " 2000 \ " Name = \ " Java \ " /> </Books> " ;
2
3 Stringbuilder sb = New Stringbuilder ();
4
5 Using (Xmlreader = Xmltextreader. Create ( New Stringreader (xmlstr )))
6 {
7 While (Xmlreader. Read ())
8 {
9 Switch (Xmlreader. nodetype)
10 {
11 Case Xmlnodetype. element:
12 If (Xmlreader. Name = " Book " )
13 {
14 If (Xmlreader. hasattributes)
15 {
16 If (Xmlreader. movetoattribute ( " Sn " ))
17 {
18SB. append (xmlreader. readcontentasint ());
19}
20 If (Xmlreader. movetoattribute ( " Name " ))
21 {
22SB. appendformat ("{0}", Xmlreader. readcontentasstring ());
23}
24
25 SB. append ( " \ R \ n " );
26 }
27 }
28 Break ;
29 }
30 }
31 }
32
33 Console. writeline (sb. tostring ());
34
35 Console. Readline ();