序列化和反序列化为XML文件,C#
我有以下代码:序列化和反序列化为XML文件,C#,c#,serialization,xml-serialization,C#,Serialization,Xml Serialization,我有以下代码: 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.GetFilesRecurs
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;
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
命名空间控制台应用程序28
{
班级计划
{
静态void Main()
{
List dirs=FileHelper.GetFilesRecursive(@“c:\Documents and Settings\bob.smith\Desktop\Test”);
foreach(dirs中的字符串p)
{
控制台写入线(p);
}
//写入计数
WriteLine(“Count:{0}”,dirs.Count);
Console.Read();
}
静态类FileHelper
{
公共静态列表GetFilesRecursive(字符串b)
{
// 1.
//将结果存储在“文件结果”列表中。
列表结果=新列表();
// 2.
//存储我们的目录堆栈。
堆栈=新堆栈();
// 3.
//添加初始目录。
堆栈推送(b);
// 4.
//在有目录要处理时继续
而(stack.Count>0)
{
//A。
//获取顶级目录
string dir=stack.Pop();
尝试
{
//B
//将此目录中的所有文件添加到结果列表中。
result.AddRange(Directory.GetFiles(dir,“*”);
//C
//在此目录中添加所有目录。
foreach(Directory.GetDirectories(dir)中的字符串dn)
{
堆栈推送(dn);
}
}
抓住
{
//D
//无法打开目录
}
}
返回结果;
}
}
}
}
上面的代码可以很好地递归查找my c:上文件夹中的文件/目录。我试图将这段代码的结果序列化到一个XML文件中,但我不知道如何做到这一点 我的项目是:查找驱动器中的所有文件/目录,序列化为XML文件。然后,第二次运行此应用程序时,我将有两个XML文件要比较。然后,我想对第一次运行此应用程序时的XML文件进行反序列化,比较与当前XML文件的差异,并生成更改报告(即已添加、删除、更新的文件) 我希望得到一些帮助,因为我是C#的初学者,而且我在序列化和反序列化方面非常不稳定。我在编码方面有很多困难。有人能帮我吗 谢谢你的帮助。将代码缩进四个空格,以便在此处发布时将其视为代码 2:去掉try/catch块,因为它会吃掉所有的异常,包括你想知道的异常 3:你有没有尝试序列化你的结果?请编辑您的问题以显示您的尝试。提示:使用XmlSerializer类。Help#1。将代码缩进四个空格,以便在此处发布时将其视为代码 2:去掉try/catch块,因为它会吃掉所有的异常,包括你想知道的异常
3:你有没有尝试序列化你的结果?请编辑您的问题以显示您的尝试。提示:使用XmlSerializer类。该类将自身序列化和反序列化……希望这会有所帮助
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
namespace TestStuff
{
public class Configuration
{
#region properties
public List<string> UIComponents { get; set; }
public List<string> Settings { get; set; }
#endregion
//serialize itself
public string Serialize()
{
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(Configuration));
using (StreamWriter xmlTextWriter = new StreamWriter(memoryStream))
{
xs.Serialize(xmlTextWriter, this);
xmlTextWriter.Flush();
//xmlTextWriter.Close();
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
memoryStream.Seek(0, SeekOrigin.Begin);
StreamReader reader = new StreamReader(memoryStream);
return reader.ReadToEnd();
}
}
//deserialize into itself
public void Deserialize(string xmlString)
{
String XmlizedString = null;
using (MemoryStream memoryStream = new MemoryStream())
{
using (StreamWriter w = new StreamWriter(memoryStream))
{
w.Write(xmlString);
w.Flush();
XmlSerializer xs = new XmlSerializer(typeof(Configuration));
memoryStream.Seek(0, SeekOrigin.Begin);
XmlReader reader = XmlReader.Create(memoryStream);
Configuration currentConfig = (Configuration)xs.Deserialize(reader);
this.Settings = currentConfig.Settings;
this.UIComponents = currentConfig.UIComponents;
w.Close();
}
}
}
static void Main(string[] args)
{
Configuration thisConfig = new Configuration();
thisConfig.Settings = new List<string>(){
"config1", "config2"
};
thisConfig.UIComponents = new List<string>(){
"comp1", "comp2"
};
//serializing the object
string serializedString = thisConfig.Serialize();
Configuration myConfig = new Configuration();
//deserialize into myConfig object
myConfig.Deserialize(serializedString);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
使用System.Xml.Serialization;
使用System.Xml;
名称空间测试
{
公共类配置
{
#区域属性
公共列表组件{get;set;}
公共列表设置{get;set;}
#端区
//序列化自身
公共字符串序列化()
{
MemoryStream MemoryStream=新的MemoryStream();
XmlSerializer xs=新的XmlSerializer(类型(配置));
使用(StreamWriter xmlTextWriter=新StreamWriter(memoryStream))
{
序列化(xmlTextWriter,this);
xmlTextWriter.Flush();
//xmlTextWriter.Close();
memoryStream=(memoryStream)xmlTextWriter.BaseStream;
memoryStream.Seek(0,SeekOrigin.Begin);
StreamReader=新的StreamReader(memoryStream);
返回reader.ReadToEnd();
}
}
//反序列化为自身
public void反序列化(字符串xmlString)
{
字符串XmlizedString=null;
使用(MemoryStream MemoryStream=new MemoryStream())
{
使用(StreamWriter w=新StreamWriter(memoryStream))
{
w、 写入(xmlString);
w、 冲洗();
XmlSerializer xs=新的XmlSerializer(类型(配置));
memoryStream.Seek(0,SeekOrigin.Begin);
XmlReader=XmlReader.Create(memoryStream);
配置currentConfig=(配置)xs.反序列化(读取器);
this.Settings=currentConfig.Settings;
this.UIComponents=currentConfig.UIComponents;
w、 Close();
}
}
}
静态void Main(字符串[]参数)
{
配置thisConfig=新配置();
thisConfig.Settings=新列表(){
“配置1”、“配置2”
};
thisConfig.UIComponents=新列表(){
“comp1”、“comp2”
};
//序列化对象
string serializedString=thisConfig.Serialize();
配置myConfig=新配置();
//反序列化为myConfig对象
myConfig.反序列化(serializedString);
[Serializable]
class Filelist: List<string> { }
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);
}
XmlSerializer serial = new XmlSerializer(typeof(FileInfo[]));
StringWriter writer = new StringWriter();
FileInfo[] fileInfoArray = GetFileInfos();
serial.Serialize(writer, fileInfoArrays);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sample
{
[Serializable()]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false, ElementName = "rootnode")]
public partial class RootNode
{
[System.Xml.Serialization.XmlElementAttribute("collection1")]
public List<OuterCollection> OuterCollections { get; set; }
}
[Serializable()]
public partial class OuterCollection
{
[XmlAttribute("attribute1")]
public string attribute1 { get; set; }
[XmlArray(ElementName = "innercollection1")]
[XmlArrayItem("text", Type = typeof(InnerCollection1))]
public List<InnerCollection1> innerCollection1Stuff { get; set; }
[XmlArray("innercollection2")]
[XmlArrayItem("text", typeof(InnerCollection2))]
public List<InnerCollection2> innerConnection2Stuff { get; set; }
}
[Serializable()]
public partial class InnerCollection2
{
[XmlText()]
public string text { get; set; }
}
public partial class InnerCollection1
{
[XmlText()]
public int number { get; set; }
}
}