Beispiel #1
0
        private static string TableNamespace(TableXsd tableXsd)
        {
            if (tableXsd == null)
            {
                throw new ArgumentNullException(nameof(tableXsd));
            }

            return(tableXsd.Schema.TargetNamespace);
        }
Beispiel #2
0
        public TableXml(TableXsd tableXsd, FileInfo path, FileIndex fileIndex, int tableNo)
            : base(path, fileIndex, tableXsd.Schema, TableNamespace(tableXsd), TableNamespaceLocation(TableNamespace(tableXsd), tableNo))
        {
            path.Refresh();
            if (path.Exists == false)
            {
                // We will now use Document from the base class.
                return;
            }

            // We will now void to load large XML documents (> 32MB) into
            // the memory because this can make an OutOfMemoryException
            // Therefore we will set the Document to NULL and use
            // a XmlTextWriter instead.
            long maxSizeForUsingXmlDocument = 1024 * 1024 * 32;

            if (path.Length < maxSizeForUsingXmlDocument)
            {
                try
                {
                    using (var fileStream = new FileStream(path.FullName, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
                    {
                        using (var xmlReader = XmlReader.Create(fileStream, CreateXmlReaderSettings(base.Schema, ValidationType.None)))
                        {
                            Document.Load(xmlReader);
                            xmlReader.Close();
                        }
                    }
                    return;
                }
                catch (OutOfMemoryException)
                {
                    Document = null;
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
            }

            Document = null;

            _shadowFile = new FileInfo($"{path.FullName}.shadow");
            try
            {
                DeleteFile(_shadowFile);

                _shadowFileStream = new FileStream(_shadowFile.FullName, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.Read);
                _xmlTextWriter    = new XmlTextWriter(_shadowFileStream, Encoding.UTF8)
                {
                    Formatting = Formatting.Indented
                };

                using (var sourceFileStream = new FileStream(path.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    using (var xmlReader = XmlReader.Create(sourceFileStream, CreateXmlReaderSettings(base.Schema, ValidationType.None)))
                    {
                        while (xmlReader.Read())
                        {
                            switch (xmlReader.NodeType)
                            {
                            case XmlNodeType.Element:
                                _xmlTextWriter.WriteStartElement(xmlReader.Prefix, xmlReader.LocalName, xmlReader.NamespaceURI);
                                _xmlTextWriter.WriteAttributes(xmlReader, true);
                                if (xmlReader.IsEmptyElement)
                                {
                                    _xmlTextWriter.WriteEndElement();
                                }
                                break;

                            case XmlNodeType.Text:
                                _xmlTextWriter.WriteString(xmlReader.Value);
                                break;

                            case XmlNodeType.Whitespace:
                            case XmlNodeType.SignificantWhitespace:
                                _xmlTextWriter.WriteWhitespace(xmlReader.Value);
                                break;

                            case XmlNodeType.CDATA:
                                _xmlTextWriter.WriteCData(xmlReader.Value);
                                break;

                            case XmlNodeType.EntityReference:
                                _xmlTextWriter.WriteEntityRef(xmlReader.Name);
                                break;

                            case XmlNodeType.XmlDeclaration:
                            case XmlNodeType.ProcessingInstruction:
                                _xmlTextWriter.WriteProcessingInstruction(xmlReader.Name, xmlReader.Value);
                                break;

                            case XmlNodeType.DocumentType:
                                _xmlTextWriter.WriteDocType(xmlReader.Name, xmlReader.GetAttribute("PUBLIC"), xmlReader.GetAttribute("SYSTEM"), xmlReader.Value);
                                break;

                            case XmlNodeType.Comment:
                                _xmlTextWriter.WriteComment(xmlReader.Value);
                                break;

                            case XmlNodeType.EndElement:
                                if (string.Compare(xmlReader.LocalName, "table", StringComparison.Ordinal) == 0)
                                {
                                    break;
                                }
                                _xmlTextWriter.WriteFullEndElement();
                                break;
                            }
                        }
                    }
                }
            }
            catch
            {
                _xmlTextWriter?.Close();
                _shadowFileStream?.Close();
                _shadowFileStream?.Dispose();

                DeleteFile(_shadowFile);

                throw;
            }
        }