c# - 部分修改 XML 序列化文档

标签 c# .net c#-3.0 xml-serialization

我有一个 XML 文档,实际上有几个,可以通过前端 UI 进行编辑。我发现了这种方法的一个问题(除了它使用 xml 文件而不是数据库这一事实……但我现在无法更改)。

如果一个用户进行了更改,而另一个用户正在进行更改,则第二个用户的更改将覆盖第一个用户。

我需要能够从 xml 文件请求对象,更改它们,然后将更改提交回 xml 文件,而无需重写整个文件。我已经在这里发布了我的整个 xml 访问类(感谢 stackoverflow 的出色帮助!)

using System;
using System.Linq;

using System.Collections;
using System.Collections.Generic;

namespace Repositories
{
    /// <summary>
    /// A file base repository represents a data backing that is stored in an .xml file.
    /// </summary>
    public partial class Repository<T> : IRepository
    {
        /// <summary>
        /// Default constructor for a file repository
        /// </summary>
        public Repository() { }

        /// <summary>
        /// Initialize a basic repository with a filename. This will have to be passed from a context to be mapped.
        /// </summary>
        /// <param name="filename"></param>
        public Repository(string filename)
        {
            FileName = filename;
        }

        /// <summary>
        /// Discovers a single item from this repository.
        /// </summary>
        /// <typeparam name="TItem">The type of item to recover.</typeparam>
        /// <typeparam name="TCollection">The collection the item belongs to.</typeparam>
        /// <param name="expression"></param>
        /// <returns></returns>
        public TItem Single<TItem, TCollection>(Predicate<TItem> expression)
            where TCollection : IDisposable, IEnumerable<TItem>
        {
            using (var list = List<TCollection>())
            {
                return list.Single(i => expression(i));
            }
        }

        /// <summary>
        /// Discovers a collection from the repository,
        /// </summary>
        /// <typeparam name="TCollection"></typeparam>
        /// <returns></returns>
        public TCollection List<TCollection>() 
            where TCollection : IDisposable
        {
            using (var list = System.Xml.Serializer.Deserialize<TCollection>(FileName))
            {
                return (TCollection)list;
            }
        }

        /// <summary>
        /// Discovers a single item from this repository.
        /// </summary>
        /// <typeparam name="TItem">The type of item to recover.</typeparam>
        /// <typeparam name="TCollection">The collection the item belongs to.</typeparam>
        /// <param name="expression"></param>
        /// <returns></returns>
        public List<TItem> Select<TItem, TCollection>(Predicate<TItem> expression)
            where TCollection : IDisposable, IEnumerable<TItem>
        {
            using (var list = List<TCollection>())
            {
                return list.Where( i => expression(i) ).ToList<TItem>();
            }
        }

        /// <summary>
        /// Attempts to save an entire collection.
        /// </summary>
        /// <typeparam name="TCollection"></typeparam>
        /// <param name="collection"></param>
        /// <returns></returns>
        public Boolean Save<TCollection>(TCollection collection)
        {
            try
            {
                // load the collection into an xml reader and try to serialize it.
                System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
                xDoc.LoadXml(System.Xml.Serializer.Serialize<TCollection>(collection));

                // attempt to flush the file
                xDoc.Save(FileName);

                // assume success
                return true;
            }
            catch
            {
                return false;
            }
        }


        internal string FileName { get; private set; }
    }

    public interface IRepository
    {
        TItem Single<TItem, TCollection>(Predicate<TItem> expression) where TCollection : IDisposable, IEnumerable<TItem>;
        TCollection List<TCollection>() where TCollection : IDisposable;
        List<TItem> Select<TItem, TCollection>(Predicate<TItem> expression) where TCollection : IDisposable, IEnumerable<TItem>;

        Boolean Save<TCollection>(TCollection collection);
    }
}

最佳答案

Xml 不是固定宽度的记录格式。在不重写的情况下很难(阅读:不要打扰)编辑内部结构。在 xml 的大多数合理用例中,这很好并且符合预期。如果这是一个问题,请考虑另一种存储机制(突然想到数据库),并在需要时简单地导入/导出 xml。

关于c# - 部分修改 XML 序列化文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2480114/

相关文章:

c# - ASP.NET MVC (.NET 4.5.1) 中的 X-Frame-Options 错误

c# - 这还是闭包吗?

c# - 在运行时不是特定于版本的 Visual Studio 项目引用

java - .Net 的客户端和服务器环境是否不同?

c# - Windows 窗体应用程序中的内存泄漏

c# - 这是一个有效的Decorator模式示例

c# - 如何使用线程处理多个任务

c# - C# 中的空 "if"语句是否会导致错误或警告?

3 层 LINQ

c# - NLog:从 nlog.config 切换到编程配置