使用XML文件在C#中存储数据
我基本上都在寻找有人指出我正确的方向。 我阅读了一些Microsoft文档,但这不是很有帮助。 这是我第一次尝试使用XML。
我正在编写一个需要存储已知用户列表的应用程序以及每个用户创建的别名列表。 我已经弄清楚如何在应用程序关闭时将我的列表序列化并存储到XML文件中,并让它在应用程序再次打开时检索它们,但我不想将用户和别名列表保留在内存中。
为了实现这一点,我需要能够在运行时搜索,编辑和附加到XML文件。
我设想的XML结构是这样的:
Username-1 Alias-1 6135551234 Alias-2 6131238888
每个用户只有一个用户名,但可能有多个别名。 我需要能够添加用户,向新用户或现有用户添加别名并更改现有别名。 用户名永远不会改变,可以删除整个用户记录。
到目前为止,我在C#中使用的唯一XML使用了序列化,但我不认为这种方法适用于上述方法。
private void WriteXML() { try { System.Xml.Serialization.XmlSerializer XMLwriter = new System.Xml.Serialization.XmlSerializer(typeof(MessageRecord)); System.IO.StreamWriter XMLfile = new System.IO.StreamWriter("Saved MessageRecords.xml"); foreach (MessageRecord mr in OutgoingMessages) { XMLwriter.Serialize(XMLfile, mr); } XMLfile.Close(); } catch (Exception e) { MessageBox.Show(e.Message); } }
创建两个类来表示UserRecord
和AliasRecord
。
public class UserRecord { public string User { get; set; } public List AliasRecords { get; set; } } public class AliasRecord { public string Alias { get; set; } public string Number { get; set; } }
像这样填充它们:
var userRecord = new UserRecord { User = "UserName1", AliasRecord = new List { new AliasRecord { Alias = "Alias1", Number = "12345678" }, new AliasRecord { Alias = "Alias2", Number = "23456789" } } };
并使用此代码序列化/反序列化它:
public static class XmlHelper { public static bool NewLineOnAttributes { get; set; } /// /// Serializes an object to an XML string, using the specified namespaces. /// public static string ToXml(object obj, XmlSerializerNamespaces ns) { Type T = obj.GetType(); var xs = new XmlSerializer(T); var ws = new XmlWriterSettings { Indent = true, NewLineOnAttributes = NewLineOnAttributes, OmitXmlDeclaration = true }; var sb = new StringBuilder(); using (XmlWriter writer = XmlWriter.Create(sb, ws)) { xs.Serialize(writer, obj, ns); } return sb.ToString(); } /// /// Serializes an object to an XML string. /// public static string ToXml(object obj) { var ns = new XmlSerializerNamespaces(); ns.Add("", ""); return ToXml(obj, ns); } /// /// Deserializes an object from an XML string. /// public static T FromXml(string xml) { XmlSerializer xs = new XmlSerializer(typeof(T)); using (StringReader sr = new StringReader(xml)) { return (T)xs.Deserialize(sr); } } /// /// Deserializes an object from an XML string, using the specified type name. /// public static object FromXml(string xml, string typeName) { Type T = Type.GetType(typeName); XmlSerializer xs = new XmlSerializer(T); using (StringReader sr = new StringReader(xml)) { return xs.Deserialize(sr); } } /// /// Serializes an object to an XML file. /// public static void ToXmlFile(Object obj, string filePath) { var xs = new XmlSerializer(obj.GetType()); var ns = new XmlSerializerNamespaces(); var ws = new XmlWriterSettings { Indent = true, NewLineOnAttributes = NewLineOnAttributes, OmitXmlDeclaration = true }; ns.Add("", ""); using (XmlWriter writer = XmlWriter.Create(filePath, ws)) { xs.Serialize(writer, obj); } } /// /// Deserializes an object from an XML file. /// public static T FromXmlFile (string filePath) { StreamReader sr = new StreamReader(filePath); try { var result = FromXml (sr.ReadToEnd()); return result; } catch (Exception e) { throw new Exception("There was an error attempting to read the file " + filePath + "\n\n" + e.InnerException.Message); } finally { sr.Close(); } } }
用法示例:
var result = XmlHelper.ToXml(userRecord);
结果:
Username1 Alias1 12345678 Alias2 23456789