stora,关于IsolatedStorageFile存取文件的问题

先看下这个类吧
1 using System; 2 using System.Net; 3 using System.Windows; 4 using System.Windows.Controls; 5 using System.Windows.Documents; 6 using System.Windows.Ink; 7 using System.Windows.Input; 8 using System.Windows.Media; 9 using System.Windows.Media.Animation; 10 using System.Windows.Shapes; 11 using System.Collections.ObjectModel; 12 using System.IO.IsolatedStorage; 13 using System.IO; 14 using System.Xml.Linq; 15 using System.Collections.Generic; 16 using System.ComponentModel; 17 18 19 namespace MvvmLightCourse.Model 20 { 21 public class CourseCollection 22 { 23 private IsolatedStorageFile isolateFile = IsolatedStorageFile.GetUserStoreForApplication(); 24 private static ObservableCollection _courseContent=new ObservableCollection(); 25 public ObservableCollection courseContent 26 { 27 get { return _courseContent; } 28 29 } 30 public CourseCollection() 31 { 32 //generateCourseContent(); 33 CreateAndReadFromXml(); 34 } 35 private void CreateAndReadFromXml() 36 { 37 isolateFile.DeleteFile("Course.xml"); 38 if (isolateFile.FileExists("Course.xml")) { MessageBox.Show("文件存在"); } 39 if (!isolateFile.FileExists("Course.xml")) 40 { 41 IsolatedStorageFileStream location = new IsolatedStorageFileStream("Course.xml", FileMode.Create, isolateFile); 42 List xeList = new List(); 43 //初试化课程xml文档 44 for (int i = 1; i <= 7; i++) 45 { 46 XElement xe = new XElement("W" + i.ToString(), 47 new XElement("ID", i), 48 new XElement("C8_10", ""), 49 new XElement("C10_12", ""), 50 new XElement("C13_15", ""), 51 new XElement("C15_17", ""), 52 new XElement("C17_19", ""), 53 new XElement("C19_21", "") 54 ); 55 xeList.Add(xe); 56 } 57 XDocument xdoc = new XDocument(new XDeclaration("1.0", "gb2312", "yes"),new XComment("this is courselist"), new XElement("root", xeList)); 58 StreamWriter fileWriter = new StreamWriter(location); 59 xdoc.Save(fileWriter); 60 MessageBox.Show(xdoc.ToString()); 61 fileWriter.Dispose(); 62 } 63 IsolatedStorageFileStream location1 = new IsolatedStorageFileStream("Course.xml", FileMode.Open,FileAccess.Read, isolateFile); 64 //开始读xml文档 65 StreamReader fileReader = new StreamReader(location1); 66 MessageBox.Show(fileReader.ReadToEnd()); 67 XDocument xdocReader = XDocument.Load(fileReader); 68 MessageBox.Show(xdocReader.ToString()); 69 //var Course = from CourseInfo in xdocReader.Descendants("") select CourseInfo; 70 foreach (var courseEle in xdocReader.Element("root").Elements()) 71 { 72 Course c = new Course(); 73 c.ID = Convert.ToString(courseEle.Element("ID").Value); 74 c.C8_10 = Convert.ToString(courseEle.Element("C8_10").Value); 75 c.C10_12 = Convert.ToString(courseEle.Element("C10_12").Value); 76 c.C13_15 = Convert.ToString(courseEle.Element("C13_15").Value); 77 c.C15_17 = Convert.ToString(courseEle.Element("C15_17").Value); 78 c.C17_19 = Convert.ToString(courseEle.Element("C17_19").Value); 79 c.C19_21 = Convert.ToString(courseEle.Element("C19_21").Value); 80 _courseContent.Add(c); 81 } 82 fileReader.Dispose(); 83 isolateFile.Dispose(); 84 } 85 public static void Save() 86 { 87 int i = 0; 88 IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication(); 89 if (! isoFile.FileExists("Course.xml")) { MessageBox.Show("this course.xml file not exists"); } 90 IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("Course.xml",FileMode.Open,isoFile); 91 StreamReader sr = new StreamReader(isoStream); 92 XDocument xdoc = XDocument.Load(sr); 93 XElement xele = xdoc.Element("root"); 94 foreach (Course course in _courseContent) 95 { 96 i+=1; 97 XElement xele1 = xele.Element("W" + i); 98 xele1.Element("C8_10").Value = course.C8_10; 99 xele1.Element("C10_12").Value = course.C10_12; 100 xele1.Element("C13_15").Value = course.C13_15; 101 xele1.Element("C15_17").Value = course.C15_17; 102 xele1.Element("C17_19").Value = course.C17_19; 103 xele1.Element("C19_21").Value = course.C19_21; 104 105 } 106 StreamWriter sw = new StreamWriter(isoStream); 107 xdoc.Save(sw); 108 isoStream.Dispose(); 109 isoFile.Dispose(); 110 } 111 112 private void generateCourseContent() 113 { 114 //throw new NotImplementedException(); 115 //_courseContent.Add(new Course("中关村1","五道口1","中关村","12","","数码大厦")); 116 //_courseContent.Add(new Course("中关村2", "五道2口", "中关村", "122", "", "数码大厦")); 117 //_courseContent.Add(new Course("中关村3", "五道口3", "中关村", "dsa", "", "数码大厦")); 118 //_courseContent.Add(new Course("中关村4", "五道口4", "中关村", "fdsa", "", "数码大厦")); 119 //_courseContent.Add(new Course("中关村5", "五道口5", "中关村", "322", "", "数码大厦")); 120 //_courseContent.Add(new Course("中关村6", "五道口6", "中关村", "3dsw", "", "数码大厦")); 121 //_courseContent.Add(new Course("中关村7", "五道口7", "中关村", "22sa", "", "数码大厦")); 122 123 } 124 125 } 126 }

问题:1、代码37行是每次都执行,在60行处显示关于IsolatedStorageFile存取文件的问题stora,为什么不显示declare的声明呢?
2、在66行处显示关于IsolatedStorageFile存取文件的问题stora,我明明new XDeclaration("1.0", "gb2312", "yes"),怎么是utf-8?
3、在67行就开始报错“没有root元素”关于IsolatedStorageFile存取文件的问题stora
希望高人指教下,我是想不白了,也调试不成功。谢谢
Tags:  isolated stora

延伸阅读

最新评论

发表评论