问题描述
我有以下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication28
{
class Program
{
static void Main()
{
List<string> dirs = FileHelper.GetFilesRecursive(@"c:\Documents and Settings\bob.smith\Desktop\Test");
foreach (string p in dirs)
{
Console.WriteLine(p);
}
//Write Count
Console.WriteLine("Count: {0}", dirs.Count);
Console.Read();
}
static class FileHelper
{
public static List<string> GetFilesRecursive(string b)
{
// 1.
// Store results in the file results list.
List<string> result = new List<string>();
// 2.
// Store a stack of our directories.
Stack<string> stack = new Stack<string>();
// 3.
// Add initial directory.
stack.Push(b);
// 4.
// Continue while there are directories to process
while (stack.Count > 0)
{
// A.
// Get top directory
string dir = stack.Pop();
try
{
// B
// Add all files at this directory to the result List.
result.AddRange(Directory.GetFiles(dir, "*.*"));
// C
// Add all directories at this directory.
foreach (string dn in Directory.GetDirectories(dir))
{
stack.Push(dn);
}
}
catch
{
// D
// Could not open the directory
}
}
return result;
}
}
}
}
上面的代码可以很好地递归查找我的 c: 上的文件夹中的文件/目录.
我正在尝试将此代码对 XML 文件执行的操作的结果序列化,但我不确定如何执行此操作.
The code above works well for recursively finding what files/directories lie in a folder on my c:.
I am trying to serialize the results of what this code does to an XML file but I am not sure how to do this.
我的项目是这样的:找到驱动器中的所有文件/目录,序列化为一个 XML 文件.然后,第二次运行这个应用程序时,我将有两个 XML 文件进行比较.然后我想从我第一次运行这个应用程序开始反序列化 XML 文件,并比较与当前 XML 文件的差异并生成更改报告(即已添加、删除、更新的文件).
My project is this: find all files/ directories w/in a drive, serialize into an XML file. Then, the second time i run this app, i will have two XML files to compare. I then want to deserialize the XML file from the first time i ran this app and compare differences to the current XML file and produce a report of changes (i.e. files that have been added, deleted, updated).
我希望得到一些帮助,因为我是 C# 的初学者,而且我在序列化和反序列化方面非常不稳定.我在编码时遇到了很多麻烦.有人可以帮我吗?
I was hoping to get some help as I am a beginner in C# and i am very very shaky on serializing and deserializing. I'm having lots of trouble coding. Can someone help me?
谢谢
推荐答案
您的结果是 List
并且不能直接序列化.你必须包装它,一种最小的方法:
Your result is List<string>
and that is not directly serializable. You'll have to wrap it, a minimal approach:
[Serializable]
class Filelist: List<string> { }
然后(去)序列化是这样的:
And then the (De)Serialization goes like:
Filelist data = new Filelist(); // replaces List<string>
// fill it
using (var stream = File.Create(@".\data.xml"))
{
var formatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
formatter.Serialize(stream, data);
}
data = null; // lose it
using (var stream = File.OpenRead(@".\data.xml"))
{
var formatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
data = (Filelist) formatter.Deserialize(stream);
}
但请注意,您不会以任何方式比较 XML(不实用).您将比较(反序列化) List 实例.而且 XML 是 SOAP 格式的,看看它.在其他情况下它可能不是很有用.
But note that you will not be comparing the XML in any way (not practical). You will compare (deserialzed) List instances. And the XML is SOAP formatted, take a look at it. It may not be very useful in another context.
因此,您可以轻松使用不同的格式化程序(二进制更高效和灵活).
And therefore you could easily use a different Formatter (binary is a bit more efficient and flexible).
或者您可能只想将文件列表持久化为 XML.那是一个不同的问题.
Or maybe you just want to persist the List of files as XML. That is a different question.
这篇关于序列化和反序列化成 XML 文件,C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!