0
我真的需要你的帮助。递归序列化方法
其实,我从一个路径读取了一些文件,并对它们进行了序列化。到现在为止,没什么特别的 NOW:我喜欢在我的例子主要文件,并子文件(新内容):
disktest.xml // Main file
disktest.1.xml //Sub file
disktest.2.xml //Sub file
MYFILE类看起来是这样的:
public class myFile
{
// Some XmlArguments like name, blablabla (xml elements)
[XmlAttribute]
public string Name {get; set;}
[XmlAttribute]
public string Version {get; set;}
[XmlAttribute]
public string Description{get; set;}
...
// And a list of my subfiles
[XmlIgnore]
public List<myFile> SubFiles {get; set;}
}
public myFile()
{
name = "";
...
SubFiles = new List<myFile>();
}
喜欢我在我的道路让所有的文件我用的LoadFile()serilize他们
// Read all Files and deserialize them
List<string> allfiles = Directory.GetFiles("path", "pattern").ToList();
List<myFile> files= allfiles.Select(f => myFile.LoadFile(f)).ToList();
public static myFile LoadFile(string fileName)
{
myFile ret = new myFile();
try
{
using (StreamReader sr = new StreamReader(fileName))
{
XmlSerializer ser = new XmlSerializer(typeof(myFile));
ret = (myFile)ser.Deserialize(sr);
}
}
catch (Exception ex)
{
ret.IsError=true;
ret.Error=ex.Message;
}
return ret;
}
的想法是,当我读到一个子文件,它有它的主文件中被序列化,这与递归性做出..问题是,I D ont找到“Base Case”来停止递归和我变得疯狂......
任何帮助对我来说都会很棒...... 在此先感谢!
嗨LB2感谢您的快速答案,每个文件具有不同的属性喜欢,以及更多的东西,如果它的主要或子文件发生变化..我需要序列化每个文件的内容,如果文件是一个子文件,它必须在同一个对象中返回... –
minoyo
@minoyo不幸的是,我不能按照你的评论,如果你不想要子文件的属性序列化,那么将它们标记为忽略。或者,如果你不想要包括子文件属性,但只是文件内容,你可以保持忽略原样,并在你的主文件中创建一个返回子文件内容的属性,并对其进行序列化。在prope的setter上rty,当内容被反序列化器分配给它时,写它以便它从文本重新创建子文件类和它的所有属性。 – LB2