public Task Save(Taglist ToSave, Lock Lock) { Initialize(); TaglistRepositoryLock DataFileLock = Lock as TaglistRepositoryLock; if (DataFileLock is null) { throw new ArgumentException("The supplied Lock was not returned by this class"); } if (DataFileLock.Released) { throw new ArgumentException("The supplied Lock has been released"); } try{ XDocument DataDocument = DataFileLock.LoadedDataDocument; XElement OldTaglistElement = ( from TL in DataDocument.Root.Elements(xmlns + "Taglist") where ToSave.Name.Equals( ((string)TL.Attribute("Name")).Normalize(NormalizationForm.FormKD), StringComparison.Ordinal ) select TL ).FirstOrDefault(); if (!(OldTaglistElement is null)) { OldTaglistElement.Remove(); } XElement NewTaglistElement = ToTaglistXML(ToSave); DataDocument.Root.Add(NewTaglistElement); return(XMLDataFileHandler.Save(DataDocument, SavingOptions, DataFileLock.Decorated)); }finally{ DataFileLock.Release(); } }
/* * The following types can be converted to XML SimpleTypes, * and as such are valid Content for XElement/XAttribute objects: * <//docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/valid-content-of-xelement-and-xdocument-objects3> * - string * - bool * - float * - double * - decimal * - DateTime * - DateTimeOffset * - TimeSpan * ToString is called for other types; IEnumerable types have their constituent elements added */ /* * The following types can be converted to from XML SimpleTypes, * such as XAttribute objects or XElement with Simple Content: * <//docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/how-to-retrieve-the-value-of-an-element-linq-to-xml> * - Nullable<T> of any of the below * - string * - bool * - int * - uint * - long * - ulong * - float * - double * - decimal * - DateTime * - TimeSpan * - GUID */ public async Task <IReadOnlyDictionary <string, Taglist> > ReadAllHeaders() { XDocument DataDocument = await XMLDataFileHandler.LoadFile(FileAccess.Read, FileShare.Read, true); IEnumerable <Taglist> Results = from TL in DataDocument.Root.Elements(xmlns + "Taglist") select new Taglist( (string)TL.Attribute("Name"), (ulong)TL.Attribute("SafeArchiveChannelID"), (ulong)TL.Attribute("QuestionableArchiveChannelID"), (ulong)TL.Attribute("ExplicitArchiveChannelID"), ImmutableHashSet <TaglistRegisteredUser> .Empty ) ; return(TaglistsDictionary(Results)); }
protected async Task <Tuple <Taglist, Lock> > Load(string TaglistName, bool LockDataFile) { TaglistName = TaglistName.Normalize(NormalizationForm.FormKD); XDocument DataDocument; Lock DataFileLock = null; if (LockDataFile) { var LoadResult = await XMLDataFileHandler.LoadFileAndLock(FileAccess.ReadWrite, FileShare.None, true); DataDocument = LoadResult.Item1; DataFileLock = LoadResult.Item2; } else { DataDocument = await XMLDataFileHandler.LoadFile(FileAccess.ReadWrite, FileShare.None, true); } bool success = false; try{ Taglist Result = ( from TL in DataDocument.Root.Elements(xmlns + "Taglist") where TaglistName.Equals( ((string)TL.Attribute("Name")).Normalize(NormalizationForm.FormKD), StringComparison.Ordinal ) select FromTaglistXML(TL) ).FirstOrDefault(); if (Result is null) { throw new EntityNotFoundException(); } success = true; return(new Tuple <Taglist, Lock>(Result, new TaglistRepositoryLock(DataFileLock, DataDocument))); }finally{ if (!success) { DataFileLock?.Release(); } } }