使用XML文件在c#中存储数据

本文关键字:存储 数据 XML 文件 使用 | 更新日期: 2023-09-27 18:18:36

我基本上是在寻找有人指出我在正确的方向。我通读了一些微软的文档,但没有多大帮助。这是我第一次尝试使用XML。

我正在编写一个应用程序,需要存储已知用户列表和每个用户创建的别名列表。我已经弄清楚了如何在应用程序关闭时将我的列表序列化并存储到XML文件中,并在应用程序再次打开时检索这些列表,但我不想将用户和别名列表保留在内存中。

为了使其工作,我需要能够在运行时搜索、编辑和追加XML文件。

我设想的XML结构类似于:

<UserRecord>
    <User>Username-1</User>
    <AliasRecord>
        <Alias>Alias-1</Alias>
        <Number>6135551234</Number>
    </AliasRecord>
    <AliasRecord>
        <Alias>Alias-2</Alias>
        <Number>6131238888</Number>
    </AliasRecord>
</UserRecord>

每个用户只能有一个用户名,但可以有多个别名。我需要能够添加用户、向新用户或现有用户添加别名以及更改现有别名。用户名永远不会改变,但是可以删除整个User记录。

到目前为止,我在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);
        }
    }

使用XML文件在c#中存储数据

创建两个类分别表示UserRecordAliasRecord

public class UserRecord
{
    public string User { get; set; }
    public List<AliasRecord> 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<AliasRecord> {
        new AliasRecord { Alias = "Alias1", Number = "12345678" }, 
        new AliasRecord { Alias = "Alias2", Number = "23456789" }
     }
 };

并使用以下代码对其进行序列化/反序列化:

public static class XmlHelper
{
    public static bool NewLineOnAttributes { get; set; }
    /// <summary>
    /// Serializes an object to an XML string, using the specified namespaces.
    /// </summary>
    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();
    }
    /// <summary>
    /// Serializes an object to an XML string.
    /// </summary>
    public static string ToXml(object obj)
    {
        var ns = new XmlSerializerNamespaces();
        ns.Add("", "");
        return ToXml(obj, ns);
    }
    /// <summary>
    /// Deserializes an object from an XML string.
    /// </summary>
    public static T FromXml<T>(string xml)
    {
        XmlSerializer xs = new XmlSerializer(typeof(T));
        using (StringReader sr = new StringReader(xml))
        {
            return (T)xs.Deserialize(sr);
        }
    }
    /// <summary>
    /// Deserializes an object from an XML string, using the specified type name.
    /// </summary>
    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);
        }
    }
    /// <summary>
    /// Serializes an object to an XML file.
    /// </summary>
    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, ns);
        }
    }
    /// <summary>
    /// Deserializes an object from an XML file.
    /// </summary>
    public static T FromXmlFile<T>(string filePath)
    {
        StreamReader sr = new StreamReader(filePath);
        try
        {
            var result = FromXml<T>(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);
结果:

<UserRecord>
    <User>Username1</User>
    <AliasRecords>
        <AliasRecord>
            <Alias>Alias1</Alias>
            <Number>12345678</Number>
        </AliasRecord>
        <AliasRecord>
            <Alias>Alias2</Alias>
            <Number>23456789</Number>
        </AliasRecord>
    </AliasRecords>
</UserRecord>