简体   繁体   English

C#XMLSerialization错误,System.InvalidOperationException

[英]C# XMLSerialization error, System.InvalidOperationException

i'm new to XMLSerialization, and have nobody near to help me out with it, so here is a part of code i want to XMLSerialize/Deserialize, and an error itself. 我是XMLSerialization的新手,没有人帮我解决它,所以这里是我希望XMLSerialize / Deserialize的代码的一部分,并且本身就是一个错误。

I have a three classes: 我有三个班:

public class Contacts
{
    public string country;
    public string city;
    public string street;
    public int houseNumber;
    public int flatNumber;
    public int phoneNumber;

    public Contacts(){...};

    public Contacts(string cntry, string city, string str, int houseNum, int flatNum, int phoneNum){...};
    public override string ToString(){...};
}

public class SimpleHuman
{
    public string firstName;
    public string lastName;
    public int age;
    public string sex;
    public Contacts contacts;

    public SimpleHuman(){...};

    public SimpleHuman(string fn, string ln, int a, string s, Contacts c){...};

    public override string ToString(){...};
}

public class doctor : SimpleHuman
{
    public DateTime contractExpirationDate;
    public int idNumber;
    public List<int> doctorPatients;

    public doctor(){...};

    public doctor(string name, string surname, int age, string sex, int doctorID, Contacts c):base(name, surname, age, sex, c){...};

    public override string ToString(){...};
}

The problem is, when i'm trying to serialize class "doctor" into XML file with: 问题是,当我尝试将类“doctor”序列化为XML文件时:

XmlSerializer SerializerDoc = new XmlSerializer(typeof(doctor));

foreach(doctor d in doctorList)
{
    TextWriter WriteFileStream = new StreamWriter("doctors/" + d.firstName + d.lastName + ".xml");
    SerializerDoc.Serialize(WriteFileStream, d);

    WriteFileStream.Close();
}

i get this error on line with creating a TextWriter (translation from russina's below): 我在创建TextWriter时会遇到此错误(从russina下面翻译):

"Необработанное исключение: System.InvalidOperationException: Clinic.doctor недос тупен в силу его уровня защиты. Возможна обработка только общих типов. в System.Xml.Serialization.TypeDesc.CheckSupported() в System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source , Boolean directReference, Boolean throwOnError) в System.Xml.Serialization.ModelScope.GetTypeModel(Type type, Boolean directR eference) в System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace) в System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNames pace) в System.Xml.Serialization.XmlSerializer..ctor(Type type) в Clinic.Clinic.saveDoctorInfo() в d:\\Dropbox\\CSharpHomeWork\\ExamTask\\Clinic\\ Program.cs:строка 833 в Clinic.Clinic.launchMenu() в d:\\Dropbox\\CSharpHomeWork\\ExamTask\\Clinic\\Prog ram.cs:строка 463 в Cli “Необработанноеисключение信息:System.InvalidOperationException:。Clinic.doctorнедоступенвсилуегоуровнязащитыВозможнаобработкатолькообщихтиповвSystem.Xml.Serialization.TypeDesc.CheckSupported()вSystem.Xml.Serialization.TypeScope.GetTypeDesc(类型类型,MemberInfo source,Boolean directReference,Boolean throwOnError)System.Xml.Serialization.ModelScope.GetTypeModel(Type type,Boolean directR eference)вSystem.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type,XmlRootAttribute root,String defaultNamespace)вSystem .Xml.Serialization.XmlSerializer..ctor(Type type,String defaultNames pace)вSystem.Xml.Serialization.XmlSerializer..ctor(Type type)Clinic.Clinic.saveDoctorInfo():d:\\ Dropbox \\ CSharpHomeWork \\ ExamTask \\ Clinic \\ Program.cs:строка863Clinic.Clinic.launchMenu():d:\\ Dropbox \\ CSharpHomeWork \\ ExamTask \\ Clinic \\ Prog ram.cs:строка463вCli nic.Program.Main(String[] args) в d:\\Dropbox\\CSharpHomeWork\\ExamTask\\Cli nic\\Program.cs:строка 870 Press any key to continue . nic.Program.Main(String [] args)•d:\\ Dropbox \\ CSharpHomeWork \\ ExamTask \\ Cli nic \\ Program.cs:строка870按任意键继续。 . ." “。

I have a russian version of WIN7, so the message on russian tells something like: "Unhandeled exception: .......: Clinic.doctor unavailable because of it's level of protection. It's possible to handle only a generic types." 我有一个俄语版的WIN7,所以俄语上的消息告诉我们:“Unhandeled exception:.......:Clinic.doctor因为它的保护级别而无法使用。它只能处理一般类型。” Translation's almost literal, so keep in mind. 翻译几乎是文字的,所以请记住。

Hope somebody will help me, because i can write class "doctor" with simple "XML Writer" but still it's none sense (as i think), as far as i can save it with one line, instead of 20. 希望有人会帮助我,因为我可以用简单的“XML Writer”编写“医生”课程,但仍然没有意义(我认为),只要我可以用一行代替20而不是它。

Thanks guys. 多谢你们。

Upadte: just did all of classes - public. Upadte:刚刚完成所有课程 - 公开。 Now i'm getting error on string: TextWriter WriteFileStream = new StreamWriter("doctors/" + d.firstName + d.lastName + ".xml"); 现在我收到字符串错误:TextWriter WriteFileStream = new StreamWriter(“doctors /”+ d.firstName + d.lastName +“。xml”);

It sais like: 

Unhandled exception: System.IO.DirectoryNotFoundException: Failed to find part of path "D:\\Dropbox\\CSharpHomeWork\\ExamTask\\Clinic\\bin\\Debug\\doctors\\Anders onNeo.xml". 未处理的异常:System.IO.DirectoryNotFoundException:无法找到路径“D:\\ Dropbox \\ CSharpHomeWork \\ ExamTask \\ Clinic \\ bin \\ Debug \\ doctors \\ Anders onNeo.xml”的一部分。

Path is 100% correct. 路径100%正确。 I just feel like instead of creating a file, it just tries to write something in already existing one. 我只是觉得不是创建一个文件,它只是尝试在已有的文件中写一些东西。

BTW, sorry that i can't thank you with reputation up, because i can't do that now. 顺便说一句,对不起,我不能用声誉来感谢你,因为我现在不能这样做。 But have my "verbal thanks" for now :) 但是现在我的“口头感谢”:)

The XmlSerializer can only serialize public classes and members. XmlSerializer只能序列化公共类和成员。 Either make the Doctor and all its base classes public class or use a different serializer (eg DataContractSerializer ). 要么将Doctor及其所有基类public class要么使用不同的序列化程序(例如DataContractSerializer )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM