예제 #1
0
        /// <summary>
        /// Save the settings to an xml file.
        /// </summary>
        /// <param name="fileName">Path of the file.</param>
        /// <param name="cancellationToken">A cancellation token.</param>
        /// <returns>A <see cref="Task"/> of the task.</returns>
        public async Task SaveAsync(string fileName, CancellationToken cancellationToken)
        {
            XDocument document = new XDocument(DefaultXmlDeclaration);
            XElement  settings = new XElement(rootName);

            document.Add(settings);
            Names.AsParallel().WithCancellation(cancellationToken).ForAll(name =>
            {
                bool mul         = IsMultiple(name);
                object propValue = this[name];
                if (mul)
                {
                    string[] values = (string[])propValue;
                    lock (syncLock)
                    {
                        settings.Add(values.Select(v => new XElement(name, v)).ToArray());
                    }
                }
                else
                {
                    lock (syncLock)
                    {
                        settings.Add(new XElement(name, propValue));
                    }
                }
            });
            using (StreamWriter writer = new StreamWriter(fileName))
            {
                await document.SaveAsync(writer, SaveOptions.None, cancellationToken);
            }
        }
예제 #2
0
        /// <summary>
        /// Open an xml file and parse it asynchronously.
        /// </summary>
        /// <param name="fileName">Path of the file.</param>
        /// <param name="cancellationToken">A cancellation token.</param>
        /// <returns>A <see cref="Task"/> of the task.</returns>
        public async Task OpenAsync(string fileName, CancellationToken cancellationToken)
        {
            XDocument document = null;

            using (StreamReader reader = new StreamReader(fileName))
            {
                document = await XDocument.LoadAsync(reader, LoadOptions.None, cancellationToken);
            }
            XElement settings = document.Element(rootName);

            Names.AsParallel().WithCancellation(cancellationToken).ForAll(name =>
            {
                bool mul         = IsMultiple(name);
                object propValue = null;
                if (mul)
                {
                    var elements = settings.Elements(name);
                    if (elements != null)
                    {
                        propValue = elements.Select(e => e.Value).ToArray();
                    }
                }
                else
                {
                    var element = settings.Element(name);
                    if (element != null)
                    {
                        propValue = element.Value;
                    }
                }
                lock (syncLock)
                {
                    this[name] = propValue;
                }
            });
        }