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

标签 c# xml database file-io

我基本上是在寻找能为我指明正确方向的人。我通读了一些 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);
        }
    }

最佳答案

创建两个类来表示 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);
        }
    }

    /// <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>

关于c# - C#中使用XML文件存储数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25368083/

相关文章:

c# - IDbCommand 是否从实现 IDisposable 的类中被释放?

c# - 为什么在监听强通配符(http ://+:port) http. sys/urlacl 绑定(bind)时 HttpListener "conflict with an existing registration"?

c# - M 后缀与十进制文字的相关性是什么

android - 下载管理器 Android 权限错误 : write external Storage

用于向数据库添加数据的 PHP 脚本不起作用

python - 我如何在 Django 中处理这样的数据库 ORM 情况?

c# - 具有动态 Entity.Property 唯一检查的通用 ValidationAttribute(在运行时设置)

c# - 为什么在我的类中实现接口(interface)时我的类方法不可见?

php - 服务器端的 CURL HTTP 身份验证

xml - XSLT 和 for-each