我正在使用C#中的应用程序。我有一个DirectorVideo类(视频导演)和一个接口类。在接口类我需要序列化视频导演列表,但我收到以下错误:
System.Xml.dll中出现未处理的“System.InvalidOperationException”类型异常
其他信息:有一个错误反映出类型'System.Collections.Generic.List`1 [ProiectC.DirectorVideo]'。
DirectorVide类:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProiectC
{ [Serializable]
public class DirectorVideo: FunctiiFisier, IComparable<DirectorVideo>, ICloneable
{
public FisierVideo[] fisiere { get; set; }
public String cale { get; set; }
public int nrFisiere { get; set; }
public DirectorVideo(FisierVideo[] fisiere, String cale, int nrFisiere) {
this.fisiere = fisiere;
this.cale = cale;
this.nrFisiere = nrFisiere;
}
public DirectorVideo(DirectorVideo dir) {
this.cale = dir.cale;
this.nrFisiere = dir.nrFisiere;
this.fisiere = dir.fisiere;
}
public int CompareTo(DirectorVideo other)
{
var caleOrder = this.cale.CompareTo(other.cale);
return caleOrder;
}
public object Clone()
{
var newDirector=(DirectorVideo)MemberwiseClone();
newDirector.cale=(String)cale.Clone();
newDirector.nrFisiere = newDirector.nrFisiere;
newDirector.fisiere = new FisierVideo[newDirector.nrFisiere];
for (int i = 0; i < newDirector.nrFisiere; i++)
newDirector.fisiere[i] = (FisierVideo)fisiere[i];
return newDirector;
}
public DirectorVideo() {
}
public DirectorVideo(string cale) {
this.cale = cale;
}
public override List<string> listFiles(string path)
{ bool ok=false;
String[] extensions = {".webm", ".mkv", ".flv", ".vob", ".drc", ".gif",
".gifv", ".mng", ".avi", ".mp4", ".wmf", "mpeg"};
DirectoryInfo d = new DirectoryInfo(path);//Assuming Test is your Folder
FileInfo[] Files = d.GetFiles("*.*"); //Getting Text files
string str = "";
List<string> files=new List<string>();
foreach (FileInfo file in Files)
{
str = file.Name;
string ext = extension(str);
ok = false;
for (int i = 0; i < extensions.Length; i++)
{
if (ext.Equals(extensions[i]))
ok = true;
}
if (ok)
files.Add(str);
}
if (files != null)
return files;
else {
files.Add("Nu a fost gasit nici un fisier video");
return files;
}
}
public override int countFiles(string path)
{
int count = 0;
bool ok = false;
String[] extensions = {".webm", ".mkv", ".flv", ".vob", ".drc", ".gif",
".gifv", ".mng", ".avi", ".mp4", ".wmf", "mpeg"};
DirectoryInfo d = new DirectoryInfo(path);//Assuming Test is your Folder
FileInfo[] Files = d.GetFiles("*.*"); //Getting Text files
string str = "";
List<string> files = new List<string>();
foreach (FileInfo file in Files)
{
str = file.Name;
string ext = extension(str);
ok = false;
for (int i = 0; i < extensions.Length; i++)
{
if (ext.Equals(extensions[i]))
ok = true;
}
if (ok)
count++;
}
return count;
}
public override string extension(string file)
{
return Path.GetExtension(file);
}
public override string ToString()
{ if (this.fisiere != null)
return string.Format("{0}", string.Join<FisierVideo>("\n", fisiere));
else
return ("Folderul nu contine fisiere video");
}
public static List<DirectorVideo> operator+(List<DirectorVideo> directoare, DirectorVideo dir)
{
directoare.Add(dir);
return directoare;
}
}
}
接口类:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace ProiectC
{ [Serializable]
public partial class Interfata : Form
{ public DirectorVideo dir { get; set; }
public List<DirectorVideo> directors { get; set; } = new List<DirectorVideo>();
public Interfata()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void btnCale_MouseClick(object sender, MouseEventArgs e)
{
DirectorVideo dir = new DirectorVideo();
string cale = txtCale.Text;
int nrFisiere = dir.countFiles(cale);
List<string> fisiereLista = dir.listFiles(cale);
FisierVideo[] fisiere = new FisierVideo[fisiereLista.Count];
for (int i = 0; i < nrFisiere; i++)
fisiere[i] = new FisierVideo(fisiereLista[i]);
DirectorVideo director = new DirectorVideo(fisiere, cale, nrFisiere);
//cloneable
this.dir = (DirectorVideo)director.Clone();
DirectorVideo combo = null;
bool ok = true;
for (int i = 0; i < comboBox.Items.Count; i++)
{ //comparable
combo = new DirectorVideo(comboBox.GetItemText(comboBox.Items[i]));
if (this.dir.CompareTo(combo) == 0)
ok = false;
}
if (ok == true)
{
comboBox.Items.Add(cale);
this.directors.Add(this.dir);
}
Console.WriteLine(directors.Count);
}
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
listView.Clear();
for (int i = 0; i < this.directors.Count; i++)
{
if (this.directors[i].cale.Equals(comboBox.Text))
{
for (int j = 0; j < this.directors[i].nrFisiere; j++)
listView.Items.Add(this.directors[i].fisiere[j].numeFisier);
}
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<DirectorVideo>));
using (StreamWriter writer = new StreamWriter("SerializedXML.xml"))
{ serializer.Serialize(writer, this.directors); }
}
}
}
答案 0 :(得分:1)
确保您尝试序列化的类的所有属性也是可序列化的,并且具有公共getter / setter。例如,您的类型FisierVideo
也需要可序列化。如果存在您不想序列化的属性,请使用XmlIgnoreAttribute
装饰它们。
[XmlIgnore]
public FisierVideo[] fisiere { get; set; }