예제 #1
0
        public static VersionedXmlLoader Load(FileInfo File)
        {
            FileStream         fs  = null;
            VersionedXmlLoader vxl = null;

            try
            {
                fs  = File.OpenRead();
                vxl = Load(fs);
            }
            catch
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
            return(vxl);
        }
예제 #2
0
        public static VersionedXmlLoader Load(Stream stream)
        {
            // Check stream
            if (stream == null)
            {
                throw new ArgumentNullException();
            }
            if (!stream.CanRead)
            {
                throw new ArgumentException("Stream can not be read");
            }
            XmlReader reader = null;

            try
            {
                // Start reading XML
                reader = XmlReader.Create(stream);
                if (reader.ReadToFollowing("Root") == false)
                {
                    return(null);
                }
                // Determine the type of the XML file
                string name    = reader.GetAttribute("Name");
                string version = reader.GetAttribute("Version");
                // XML file doesn't meet our expected format
                if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(version))
                {
                    return(null);
                }
                string type = name + "_" + version;
                // Load file as known type
                stream.Seek(0, SeekOrigin.Begin);
                Type dataType = null;
                lock (types)
                {
                    if (types.ContainsKey(type))
                    {
                        dataType = types[type];
                    }
                }
                if (dataType == null)
                {
                    return(null);
                }
                // Read data
                VersionedXmlLoader data = (VersionedXmlLoader)Xml.Compat.Deserialize(dataType, stream, false);
                if (data == null)
                {
                    return(null);
                }
                // Upgrade data if needed
                while (data.CanUpgrade)
                {
                    data = data.Upgrade();
                }
                return(data);
            }
            catch (Exception) { return(null); }
            finally
            {
                // Close anything left open
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }