XML:进程无法访问文件

时间:2016-06-17 19:04:45

标签: c# xml

我有5个列表,并希望将它们保存到XML。我的代码适用于其中4个,但我绝对不知道,但它不想要保存第五个。我为每个列表使用重载方法SaveXML。只有节省房间才有问题。我总是遇到异常:进程无法访问文件Rooms.xml ...

public class Database
{
        List<Student> dtbStudents = new List<Student>();            
        List<Teacher> dtbTeachers = new List<Teacher>();
        List<StudyProgramme> dtbPrograms = new List<StudyProgramme>();
        List<Subject> dtbSubjects = new List<Subject>();
        List<Room> dtbRooms = new List<Room>();            

        public Database()
        {
            LoadXML();
        }

        public void SaveXML()
        {
           SaveXML(dtbStudents);
           SaveXML(dtbTeachers);
           SaveXML(dtbPrograms);
           SaveXML(dtbSubjects);
           SaveXML(dtbRooms);
        }
        public void SaveXML(List<Teacher> list)
        {
             XmlSerializer serializer = new XmlSerializer(list.GetType());
             using (TextWriter writer = new StreamWriter(@"Teachers.xml"))
             {
                  serializer.Serialize(writer, list);
             }
        }
        public void SaveXML(List<StudyProgramme> list)
        {
            XmlSerializer serializer = new XmlSerializer(list.GetType());
            using (TextWriter writer = new StreamWriter(@"StudyProgrammes.xml"))
            {
            serializer.Serialize(writer, list);
            }
         }
         public void SaveXML(List<Student> list)
         {
             XmlSerializer serializer = new XmlSerializer(list.GetType());
             using (TextWriter writer = new StreamWriter(@"Students.xml"))
             {
                serializer.Serialize(writer, list);
             }
         }
         public void SaveXML(List<Subject> list)
         {
             XmlSerializer serializer = new XmlSerializer(list.GetType());
             using (TextWriter writer = new StreamWriter(@"Subjects.xml"))
             {
                 serializer.Serialize(writer, list);
             }
         }
         public void SaveXML(List<Room> list)
         {
            XmlSerializer serializer = new XmlSerializer(list.GetType());
            using (TextWriter writer = new StreamWriter(@"Rooms.xml"))
            {
            serializer.Serialize(writer, list);
            }       
         }
}

class Program
{
    static void Main(string[] args)
    {
        Database dtb = new Database();

        dtb.SaveXML();               
        Console.ReadKey();
    }
}

从xmls加载数据可能有问题或者什么?我可以发布这个功能。 谢谢你的帮助!

这是我的加载功能:

public void LoadXML()
    {
        XmlReader Reader = XmlReader.Create(@"Students.xml");
        XmlSerializer deserializer = new XmlSerializer(typeof(List<Student>));
        dtbStudents = (List<Student>)deserializer.Deserialize(Reader);

        Reader = XmlReader.Create(@"Teachers.xml");
        deserializer = new XmlSerializer(typeof(List<Teacher>));
        dtbTeachers = (List<Teacher>)deserializer.Deserialize(Reader);

        Reader = XmlReader.Create(@"StudyProgrammes.xml");
        deserializer = new XmlSerializer(typeof(List<StudyProgramme>));
        dtbPrograms = (List<StudyProgramme>)deserializer.Deserialize(Reader);

        Reader = XmlReader.Create(@"Rooms.xml");
        deserializer = new XmlSerializer(typeof(List<Room>));
        dtbRooms = (List<Room>)deserializer.Deserialize(Reader);

        Reader = XmlReader.Create(@"Subjects.xml");
        deserializer = new XmlSerializer(typeof(List<Subject>));
        dtbSubjects = (List<Subject>)deserializer.Deserialize(Reader);

        Reader.Close();
    }

1 个答案:

答案 0 :(得分:0)

LoadXML()中,您只关闭 last 读者实例:

    XmlReader Reader = XmlReader.Create(@"Students.xml");
    // Snip code for reading
    Reader = XmlReader.Create(@"Teachers.xml");
    // Snip code for reading
    Reader = XmlReader.Create(@"StudyProgrammes.xml");
    // Snip code for reading
    Reader = XmlReader.Create(@"Rooms.xml");
    // Snip code for reading

    Reader = XmlReader.Create(@"Subjects.xml");
    // Snip code for reading
    // Here the last instance is closed -- but the other four were
    // never closed.
    Reader.Close(); 

因此,当引用变量XmlReader的值发生更改时,前四个Reader实例将被放弃,因此在收集和完成垃圾回收时,最终将被关闭。如果其中一个文件在您稍后尝试回写时仍处于打开状态,因为终结器线程尚未关闭该文件,您将看到您所看到的异常。

相反,您应该在完成后立即处理每个实例:

    using (var reader = XmlReader.Create(@"Students.xml"))
    {
        XmlSerializer deserializer = new XmlSerializer(typeof(List<Student>));
        dtbStudents = (List<Student>)deserializer.Deserialize(reader);
    }

    // Similarly for the others.