Esempio n. 1
0
        /// <summary>Initialize an element stream from the XML file <paramref name="filename"/> with <see cref="owner"/> as the initial owner object</summary>
        /// <param name="filename">Name of the XML file we're to load</param>
        /// <param name="permissions">Supported access permissions for this stream</param>
        /// <param name="owner">Initial owner object</param>
        public XmlElementStream(string filename,
                                System.IO.FileAccess permissions = System.IO.FileAccess.ReadWrite, object owner = null)
        {
            Contract.Requires <ArgumentNullException>(filename != null);

            if (!System.IO.File.Exists(filename))
            {
                throw new System.IO.FileNotFoundException("XmlElementStream: Load", filename);
            }

            Document = new Xml.XmlDocumentWithLocation();
            try
            {
                var xml_reader_settings = new XmlReaderSettings
                {
                    XmlResolver   = null,
                    DtdProcessing = DtdProcessing.Ignore
                };
                using (var xml_reader = XmlReader.Create(this.StreamName = filename, xml_reader_settings))
                {
                    Document.Load(xml_reader);
                }
            }
            catch (Exception ex)
            {
                throw new System.IO.InvalidDataException("Failed to load " + StreamName, ex);
            }

            StreamMode = StreamPermissions = permissions;

            this.Owner = owner;
        }
Esempio n. 2
0
        public XmlElementStream(System.IO.Stream sourceStream,
                                System.IO.FileAccess permissions = System.IO.FileAccess.ReadWrite, object owner = null, string streamNameOverride = null)
        {
            Contract.Requires <ArgumentNullException>(sourceStream != null);
            Contract.Requires <ArgumentException>(sourceStream.HasPermissions(permissions));

            if (streamNameOverride.IsNullOrEmpty())
            {
                SetStreamName(sourceStream);
            }
            else
            {
                base.StreamName = streamNameOverride;
            }

            var doc = new Xml.XmlDocumentWithLocation
            {
                FileName = base.StreamName
            };

            Document = doc;
            try
            {
                using (var xmlReader = XmlReader.Create(sourceStream))
                {
                    Document.Load(xmlReader);
                }
            } catch (Exception ex)
            {
                throw new System.IO.InvalidDataException("Failed to load " + StreamName, ex);
            }

            StreamMode = StreamPermissions = permissions;

            this.Owner = owner;
        }