실제로 프런트 엔드 UI를 통해 편집 할 수있는 XML 문서가 있습니다. 나는이 접근 방식에 문제가 있음을 발견했다. (데이터베이스 대신 xml 파일을 사용하고 있다는 사실 외에도 ... 지금은 변경할 수 없다.)부분적으로 XML 직렬화 된 문서 수정
다른 사용자가 변경 작업을 수행하는 동안 한 사용자가 변경 한 경우 두 번째 사용자가 변경하면 첫 번째 사용자가 변경 사항을 덮어 씁니다.
XML 파일에서 개체를 요청하고 변경 한 다음 전체 파일을 다시 쓰지 않고 XML 파일로 변경 사항을 다시 제출할 수 있어야합니다. 내 전체 XML 액세스 클래스 (유래에서 멋진 도움에 감사를 형성 한!)
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을 인식하는 3 가지 방식의 병합. - 다른 것을 시도해보십시오. – dtb
한숨. 나는 정말로 모른다. 이 전체 프로젝트는 어리 석다 - 그것은 모든 데이터베이스에 의해 더 나은 서비스를 제공 할 것이지만 나는 그것을 위해 하나를 설정하고 데이터를 전송할 시간이 없다. – Ciel