XmlTextWriter myXmlTextWriter = new XmlTextWriter( _ 
   Environment.GetEnvironmentVariable("TEMP") + "\\newbooks.xml", null); 
1.                 public class Data 

2.                { 
3.                    const int kMAX_HIGHSCORES = 10; 
4.                    const string kDATA_FILE = @"\Program 
Files\PocketDice\data.xml"; 
5.
6.                    private HighScoreRecord[] _list; 
7.
8.                    public HighScoreRecord[] HighScores 
9.                    { 
10.                       get {return _list;} 
11.                   } 
12.
13.                   public Data() 
14.                   { 
15.
16.                       // ֤ǷѴһ߷ļ 
17.                       // ûУ 
18.                       _list = new HighScoreRecord[kMAX_HIGHSCORES]; 
19.                       int count = -1; 
20.                       XmlTextReader xml = null; 
21.                       FileStream stream = null; 
22.
23.                       try 
24.                       { 
25.                           stream = new FileStream(kDATA_FILE, 
FileMode.Open); 
26.                           xml = new XmlTextReader(stream); 
27.                           xml.MoveToContent(); 
28.
29.                           while (xml.Read()) 
30.                           { 
31.                               switch (xml.Name) 
32.                               { 
33.                                   case "HighScore": 
34.                                       count++; 
35.                                       if (count < kMAX_HIGHSCORES) 
36.                                       { 
37.                                            _list[count] = new 
HighScoreRecord(xml); 
38.                                       } 
39.                                       break; 
40.                               } 
41.                           } 
42.
43.                           count++; 
44.                           if (count < kMAX_HIGHSCORES) 
45.                           { 
46.                               for (int i=count; i<kMAX_HIGHSCORES; i++)
47.                               { 
48.                                   _list[i] = new HighScoreRecord(); 
49.                               } 
50.                           } 
51.
52.                       } 
53.                       catch (System.IO.FileNotFoundException) 
54.                       { 
55.                           // ļΪ֮󱣴ʼܹ 
56.                           for (int i=0; i<kMAX_HIGHSCORES; i++) 
57.                           { 
58.                               _list[i] = new HighScoreRecord(); 
59.                           } 
60.                       } 
61.
62.                       catch (Exception ex) 
63.                       { 
64.    System.Windows.Forms.MessageBox.Show(ex.ToString(), "Error Loading High Scores");
65.                       for (int i=0; i<kMAX_HIGHSCORES; i++) 
66.                       { 
67.                           _list[i] = new HighScoreRecord(); 
68.                       } 
69.                   } 
70.
71.                   finally 
72.                   { 
73.                       if (xml != null) xml.Close(); 
74.                       if (stream != null) stream.Close(); 
75.                   } 
76.
77.               } 
78.
79.               public bool IsNewHighScore(int score) 
80.               { 
81.                   // Ӧñ洢ֵֵǸ߷ 
82.                   if (_list[kMAX_HIGHSCORES - 1].Score < score) 
83.                       return true; 
84.                   else 
85.                       return false; 
86.
87.               } 
88.
89.               public int StoreHighScore(int score, string name) 
90.               { 
91.                   // 洢߷ֲ#
92.                   // of the score after sorting 
93.                   DateTime dt = DateTime.Today; 
94.
95.                   _list[kMAX_HIGHSCORES - 1].Score = score; 
96.                   _list[kMAX_HIGHSCORES - 1].Name = name; 
97.                   _list[kMAX_HIGHSCORES - 1].Date = dt; 
98.
99.                   this.SortHighScores(); 
100.
101.                  for (int i=0; i<kMAX_HIGHSCORES; i++) 
102.                  { 
103.                      if (_list[i].Score == score 
104.                          && _list[i].Name == name 
105.                          && _list[i].Date == dt) 
106.                          return i; 
107.                      } 
108.
109.                      return -1; 
110.                  } 
111.
112.                  public void SortHighScores() 
113.                  { 
114.                      HighScoreRecord temp; 
115.
116.                      for (int j=0; j<kMAX_HIGHSCORES; j++) 
117.                      { 
118.                      for (int i=0; i<kMAX_HIGHSCORES - 1; i++) 
119.                      { 
120.                          if (_list[i].Score < _list[i+1].Score) 
121.                         { 
122.                             // swap 
123.                             temp = _list[i]; 
124.                             _list[i] = _list[i+1]; 
125.                             _list[i+1] = temp; 
126.                         } 
127.                     } 
128.                 } 
129.             } 
130.
131.             public void ClearHighScores() 
132.             { 
133.                 for (int i=0; i<kMAX_HIGHSCORES - 1; i++) 
134.                 { 
135.                     _list[i].Score = -1; 
136.                     _list[i].Name = ""; 
137.                     _list[i].Date = DateTime.MinValue; 
138.                 } 
139.
140.             } 
141.
142.             public void Save() 
143.             { 
144.
145.                 // ڱ֮ǰHighScores 
146.                 SortHighScores(); 
147.
148.                 // ڳԱ; 
149.                 XmlTextWriter xml = null; 
150.
151.             try 
152.             { 
153.                 xml = new XmlTextWriter(kDATA_FILE, System.
Text.Encoding.ASCII); 
154.                 xml.WriteStartDocument(); 
155.
156.                 // дHighScoreб
157.                 xml.WriteStartElement("HighScores"); 
158.                 for (int i=0; i<_list.Length; i++) 
159.                 { 
160.                     _list[i].WriteXmlRecord(xml); 
161.                 } 
162.
163.                 xml.WriteEndElement(); // </HighScores> 
164.
165.                 xml.WriteEndDocument(); 
166.             } 
167.             catch (Exception ex) 
168.             { 
169.             System.Windows.Forms.MessageBox.Show(ex.ToString()); 
170.             } 
171.             finally 
172.            { 
173.                if (xml != null) xml.Close(); 
174.            } 
175.
176.        } 
177.
178.    } 
179.  }
