您可以使用XML反序列化和序列化要做到這一點。
/// <summary>
/// Saves to an xml file
/// </summary>
/// <param name="FileName">File path of the new xml file</param>
public void Save(string FileName)
{
using (var writer = new System.IO.StreamWriter(FileName))
{
var serializer = new XmlSerializer(this.GetType());
serializer.Serialize(writer, this);
writer.Flush();
}
}
要創建從保存文件的對象,添加以下功能並且與要創建的對象類型替換[對象類型。
/// <summary>
/// Load an object from an xml file
/// </summary>
/// <param name="FileName">Xml file name</param>
/// <returns>The object created from the xml file</returns>
public static [ObjectType] Load(string FileName)
{
using (var stream = System.IO.File.OpenRead(FileName))
{
var serializer = new XmlSerializer(typeof([ObjectType]));
return serializer.Deserialize(stream) as [ObjectType];
}
}
編號:Serialize an object to XML
如果您需要將xml轉換爲Dictionary對象,則此墨跡可能會重複您正在搜索的內容http://stackoverflow.com/questions/13952425/how-to-convert-xml-to-dictionary – Nivs
如果它是設計時轉換,可以用xsd.exe生成類 – Thangadurai
我覺得這樣會對你有所幫助:http://stackoverflow.com/questions/8950493/converting-xmldocument-to-dictionarystring-string –