gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:34:47 29 4
gpt4 key购买 nike

我有一个 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com