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

问题描述 投票:0回答:1

我基本上是在寻找有人在这方面为我指明正确的方向。我阅读了一些 Microsoft 文档,但这并没有多大帮助。这是我第一次尝试使用 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>

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

到目前为止,我在 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);
        }
    }
c# xml database file-io
1个回答
23
投票

创建两个类来表示

UserRecord
AliasRecord

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>
© www.soinside.com 2019 - 2024. All rights reserved.