Reputation: 587
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.
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:
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):
"Необработанное исключение: 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 в Clinic.Program.Main(String[] args) в d:\Dropbox\CSharpHomeWork\ExamTask\Cli nic\Program.cs:строка 870 Press any key to continue . . ."
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." 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.
Thanks guys.
Upadte: just did all of classes - public. Now i'm getting error on string: 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".
Path is 100% correct. 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 :)
Upvotes: 1
Views: 7575
Reputation: 32587
The XmlSerializer
can only serialize public classes and members. Either make the Doctor
and all its base classes public class
or use a different serializer (e.g. DataContractSerializer
).
Upvotes: 3