比较来自世界各地的卖家的域名和 IT 服务价格

易于在文件中录制一个类的整个实例。 XML 并读回来

我有一个基本的课程
theGarage

, 其中包含我们课程的副本 customer, supplier 和 jobs.

我想将程序数据保存到文件中 XML, 我使用了下面的代码 /只是一个片段,我有其他课程的合适代码/. 我想知道我是否有更简单的方式做到这一点,例如,写下整个班级 theGarage 到文件。 XML 并阅读它而无需编写所有这些代码,就像我下面的那样。


public void saveToFile//
{
using /XmlWriter writer = XmlWriter.Create/"theGarage.xml"//
{
writer.WriteStartDocument//;

///
writer.WriteStartElement/"theGarage"/;
writer.WriteStartElement/"Customers"/;

foreach /Customer Customer in Program.theGarage.Customers/
{
writer.WriteStartElement/"Customer"/;
writer.WriteElementString/"FirstName", Customer.FirstName/;
writer.WriteElementString/"LastName", Customer.LastName/;
writer.WriteElementString/"Address1", Customer.Address1/;
writer.WriteElementString/"Address2", Customer.Address2/;
writer.WriteElementString/"Town", Customer.Town/;
writer.WriteElementString/"County", Customer.County/;
writer.WriteElementString/"PostCode", Customer.Postcode/;
writer.WriteElementString/"TelephoneHome", Customer.TelephoneHome/;
writer.WriteElementString/"TelephoneMob", Customer.TelephoneMob/;

//begin vehicle list
writer.WriteStartElement/"Vehicles"/;

foreach /Vehicle Vehicle in Customer.Cars/
{
writer.WriteStartElement/"Vehicle"/;
writer.WriteElementString/"Make", Vehicle.Make/;
writer.WriteElementString/"Model", Vehicle.Model/;
writer.WriteElementString/"Colour", Vehicle.Colour/;
writer.WriteElementString/"EngineSize", Vehicle.EngineSize/;
writer.WriteElementString/"Registration", Vehicle.Registration/;
writer.WriteElementString/"Year", Vehicle.YearOfFirstReg/;
writer.WriteEndElement//;
}
writer.WriteEndElement//;
writer.WriteEndElement//;
}
}
}
已邀请:

风见雨下

赞同来自:

序列化对象有一种最简单的方法,而是使用
XmlSerializer

. 查看文档
http://msdn.microsoft.com/en-u ... .aspx
.

用于将车库序列化为文件的代码片段可能如下所示:


var garage = new theGarage//;

// TODO init your garage..

XmlSerializer xs = new XmlSerializer/typeof/theGarage//;
TextWriter tw = new StreamWriter/@"c:\temp\garage.xml"/;
xs.Serialize/tw, garage/;


和从文件加载车库的代码:


using/var sr = new StreamReader/@"c:\temp\garage.xml"//
{
garage = /theGarage/xs.Deserialize/sr/;
}

诸葛浮云

赞同来自:

这几乎很棒 extension methods, 然后你可以轻松阅读 / 写它 / 来自文件。


public static class Extensions
{
public static string ToXml/this object obj/
{
XmlSerializer s = new XmlSerializer/obj.GetType///;
using /StringWriter writer = new StringWriter///
{
s.Serialize/writer, obj/;
return writer.ToString//;
}
}

public static T FromXml<t>/this string data/
{
XmlSerializer s = new XmlSerializer/typeof/T//;
using /StringReader reader = new StringReader/data//
{
object obj = s.Deserialize/reader/;
return /T/obj;
}
}
}


例子


var xmlData = myObject.ToXml//;

var anotherObject = xmlData.FromXml<objecttype>//;


</objecttype></t>

莫问

赞同来自:

我刚写了一条消息
http://blog.danskingdom.com/sa ... file/
. 以下是用于录制和读取类实例的功能 / 的 XML. 有关更多信息,请参阅我的博客。

需要是 System.Xml assembly 包含在您的项目中。


/// <summary>
/// Writes the given object instance to an XML file.
/// <para>Only Public properties and variables will be written to the file. These can be any type though, even other classes.</para>
/// <para>If there are public properties/variables that you do not want written to the file, decorate them with the [XmlIgnore] attribute.</para>
/// <para>Object type must have a parameterless constructor.</para>
/// </summary>
/// <typeparam name="T">The type of object being written to the file.</typeparam>
/// <param name="filePath"/>The file path to write the object instance to.
/// <param name="objectToWrite"/>The object instance to write to the file.
/// <param name="append"/>If false the file will be overwritten if it already exists. If true the contents will be appended to the file.
public static void WriteToXmlFile<t>/string filePath, T objectToWrite, bool append = false/ where T : new//
{
TextWriter writer = null;
try
{
var serializer = new XmlSerializer/typeof/T//;
writer = new StreamWriter/filePath, append/;
serializer.Serialize/writer, objectToWrite/;
}
finally
{
if /writer != null/
writer.Close//;
}
}

/// <summary>
/// Reads an object instance from an XML file.
/// <para>Object type must have a parameterless constructor.</para>
/// </summary>
/// <typeparam name="T">The type of object to read from the file.</typeparam>
/// <param name="filePath"/>The file path to read the object instance from.
/// <returns>Returns a new instance of the object read from the XML file.</returns>
public static T ReadFromXmlFile<t>/string filePath/ where T : new//
{
TextReader reader = null;
try
{
var serializer = new XmlSerializer/typeof/T//;
reader = new StreamReader/filePath/;
return /T/serializer.Deserialize/reader/;
}
finally
{
if /reader != null/
reader.Close//;
}
}


例子


// Write the list of salesman objects to file.
WriteToXmlFile<customer>/"C:\TheGarage.txt", customer/;

// Read the list of salesman objects from the file back into a variable.
Customer customer = ReadFromXmlFile<customer>/"C:\TheGarage.txt"/;


</customer></customer></t></t>

江南孤鹜

赞同来自:

对于我的项目,我使用
http://msdn.microsoft.com/en-u ... .aspx
. 不像
http://msdn.microsoft.com/en-u ... .aspx
XmlSerializer, 它可以以这样的方式处理对同一对象的几个引用,使数据不复制 xml 并恢复保存。

要回复问题请先登录注册