[英]Serialize a class using XML
看了3個多小時的輔導,沒找到答案:
我正在嘗試在這里序列化一個類:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.Serialization;
namespace Gestionnaire_d_Établissement_Scolaire
{
public class Resources
{
public List<Student> Students { get; set; }
public List<Teacher> Teachers { get; set; }
public List<Etablissement> Etablissements { get; set; }
public string Path_Etablissement = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Zonedetec\Gestionnaire d'établissement Scolaire\Etablissements.xml";
public string Path_Teacher = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Zonedetec\Gestionnaire d'établissement Scolaire\Teachers.xml";
public string Path_Student = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Zonedetec\Gestionnaire d'établissement Scolaire\Students.xml";
public void LoadAll()
{
}
public void AddTeacher()
{
}
public void AddEtablissement()
{
var doc = new XDocument();
using (var writer = doc.CreateWriter())
{
var serializer = new XmlSerializer(typeof(Resources));
serializer.Serialize(writer, this);
}
doc.Save(Path_Etablissement);
}
}
}
}
但是這段代碼給了我一個錯誤如下:
System.IO.FileLoadException: 給定程序集的名稱或基本代碼無效。 (HRESULT 異常:0x80131047)
瘋狂地我迷路了,我沒有解決辦法。 它在這條線上:
var serializer = new XmlSerializer(typeof(Resources));
感謝你們對我的幫助
編輯
如果你 - 你問自己這只是我想要序列化的 3 個列表,但是否也采用了路徑變量真的無關緊要
嘗試使用 GET :
public string Path_Etablissement
{
get { return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Zonedetec\Gestionnaire d'établissement Scolaire\Etablissements.xml"; }
}
public string Path_Teacher
{
get { return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Zonedetec\Gestionnaire d'établissement Scolaire\Teachers.xml"; }
}
public string Path_Student
{
get { return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Zonedetec\Gestionnaire d'établissement Scolaire\Students.xml"; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.