Index: Core/Components/src/Core.Components.Gis.IO/Readers/WmtsConnectionInfoReader.cs =================================================================== diff -u -rf427e6dbf1ae2c7855a72a2f2ce9f458ed6167ba -r5fb648434280926b0150a289a61fbb3ca620bdbe --- Core/Components/src/Core.Components.Gis.IO/Readers/WmtsConnectionInfoReader.cs (.../WmtsConnectionInfoReader.cs) (revision f427e6dbf1ae2c7855a72a2f2ce9f458ed6167ba) +++ Core/Components/src/Core.Components.Gis.IO/Readers/WmtsConnectionInfoReader.cs (.../WmtsConnectionInfoReader.cs) (revision 5fb648434280926b0150a289a61fbb3ca620bdbe) @@ -23,7 +23,6 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; -using System.Linq; using System.Xml; using System.Xml.Linq; using Core.Common.Base.IO; @@ -60,15 +59,14 @@ { IOUtils.ValidateFilePath(path); - var readConnectionInfos = new WmtsConnectionInfo[0]; if (!File.Exists(path)) { - return new ReadOnlyCollection(readConnectionInfos); + return new ReadOnlyCollection(new WmtsConnectionInfo[0]); } try { - readConnectionInfos = ReadWmtsConnectionInfosFromFile(path).ToArray(); + return ReadWmtsConnectionInfosFromFile(path); } catch (Exception exception) when (exception is XmlException || exception is InvalidOperationException @@ -78,7 +76,6 @@ .Build(CoreCommonUtilsResources.Error_General_IO_Import_ErrorMessage); throw new CriticalFileReadException(message, exception); } - return new ReadOnlyCollection(readConnectionInfos); } /// @@ -89,27 +86,7 @@ { using (XmlReader reader = XmlReader.Create(new StringReader(Resources.defaultWmtsConnectionInfo))) { - var connectionInfos = new List(); - while (reader.Read()) - { - if (IsReadElementWmtsConnectionElement(reader)) - { - continue; - } - - WmtsConnectionInfo readWmtsConnectionElement; - using (XmlReader subtreeReader = reader.ReadSubtree()) - { - XElement wmtsConnectionElement = XElement.Load(subtreeReader); - - readWmtsConnectionElement = CreateWmtsConnectionInfo(wmtsConnectionElement); - } - if (readWmtsConnectionElement != null) - { - connectionInfos.Add(readWmtsConnectionElement); - } - } - return new ReadOnlyCollection(connectionInfos); + return ReadWntsConnectionInfosFromReader(reader, CreateWmtsConnectionInfo); } } @@ -120,33 +97,49 @@ /// The read collection. /// Thrown when an error occurred while parsing the XML. /// Thrown when an error occurred while reading the XML. - private static IEnumerable ReadWmtsConnectionInfosFromFile(string path) + private static ReadOnlyCollection ReadWmtsConnectionInfosFromFile(string path) { using (XmlReader reader = XmlReader.Create(path)) { - var connectionInfos = new List(); - while (reader.Read()) + return ReadWntsConnectionInfosFromReader(reader, element => TryCreateWmtsConnectionInfo(path, element)); + } + } + + /// + /// Reads the collection of from an . + /// + /// The reader to use. + /// Method responsible to turning an + /// into a . This method may + /// return null or throw any type of . + /// + /// This method only throws exceptions thrown by . + /// Thrown when an error occurred while parsing the XML. + /// Thrown when an error occurred while reading the XML. + private static ReadOnlyCollection ReadWntsConnectionInfosFromReader(XmlReader reader, + Func tryParseWmtsConnectionElement) + { + var connectionInfos = new List(); + while (reader.Read()) + { + if (IsReadElementWmtsConnectionElement(reader)) { - if (IsReadElementWmtsConnectionElement(reader)) - { - continue; - } + continue; + } - WmtsConnectionInfo readWmtsConnectionElement; - using (XmlReader subtreeReader = reader.ReadSubtree()) - { - XElement wmtsConnectionElement = XElement.Load(subtreeReader); + WmtsConnectionInfo readWmtsConnectionElement; + using (XmlReader subtreeReader = reader.ReadSubtree()) + { + XElement wmtsConnectionElement = XElement.Load(subtreeReader); - readWmtsConnectionElement = TryCreateWmtsConnectionInfo(path, wmtsConnectionElement); - } - - if (readWmtsConnectionElement != null) - { - connectionInfos.Add(readWmtsConnectionElement); - } + readWmtsConnectionElement = tryParseWmtsConnectionElement(wmtsConnectionElement); } - return connectionInfos; + if (readWmtsConnectionElement != null) + { + connectionInfos.Add(readWmtsConnectionElement); + } } + return new ReadOnlyCollection(connectionInfos); } /// @@ -193,7 +186,7 @@ XElement nameElement = element.Element(WmtsConnectionInfoXmlDefinitions.WmtsConnectionNameElement); XElement urlElement = element.Element(WmtsConnectionInfoXmlDefinitions.WmtsConnectionUrlElement); - return new WmtsConnectionInfo(nameElement?.Value, urlElement?.Value); + return new WmtsConnectionInfo(nameElement.Value, urlElement.Value); } } } \ No newline at end of file