Example #1
0
        /// <summary>Writes the specified records to the destination file.</summary>
        /// <param name="destinationFile">Information about the file in which the contents must be saved.</param>
        /// <param name="dataSourceInfo">Optional information about the data source.</param>
        /// <param name="contents">The new contents of the file.</param>
        /// <returns>The entities after they have been written to the file (in case the saving resulted in some updated values).</returns>
        protected override IEnumerable <TEntity> WriteAllRecordsToFile(FileInfo destinationFile, DataSourceInfo dataSourceInfo,
                                                                       IEnumerable <TEntity> contents)
        {
            Encoding encoding = this.SourceFileEncoding;

            if (FileSourceInfo.IsSourceFileEncodingSpecified(dataSourceInfo))
            {
                encoding = FileSourceInfo.SelectSourceFileEncoding(dataSourceInfo);
            }

            Stream fileStream = null;

            try {
                fileStream = new FileStream(destinationFile.FullName, FileMode.Create, FileAccess.Write);
                using (StreamWriter writer = new StreamWriter(fileStream, encoding)) {
                    fileStream = null;
                    this.serializer.Serialize(writer, contents.ToList());
                    return(contents);
                }
            }
            finally {
                if (fileStream != null)
                {
                    fileStream.Dispose();
                }
            }
        }
Example #2
0
        /// <summary>Reads all the available records from the source file.</summary>
        /// <param name="sourceFile">Information about the file that must be read.</param>
        /// <param name="dataSourceInfo">Optional information about the data source.</param>
        /// <returns>The entities that were read from the file.</returns>
        protected override IEnumerable <TEntity> ReadAllRecordsFromFile(FileInfo sourceFile, DataSourceInfo dataSourceInfo)
        {
            Encoding encoding = this.SourceFileEncoding;

            if (FileSourceInfo.IsSourceFileEncodingSpecified(dataSourceInfo))
            {
                encoding = FileSourceInfo.SelectSourceFileEncoding(dataSourceInfo);
            }

            FileStream fileStream = null;

            try {
                fileStream = new FileStream(sourceFile.FullName, FileMode.Open, FileAccess.Read);
                using (StreamReader reader = new StreamReader(fileStream, encoding)) {
                    fileStream = null;
                    IEnumerable <TEntity> result = this.serializer.Deserialize(reader) as List <TEntity>;
                    return(result);
                }
            }
            finally {
                if (fileStream != null)
                {
                    fileStream.Dispose();
                }
            }
        }
Example #3
0
        /// <summary>Reads all the available records from the source file.</summary>
        /// <param name="sourceFile">Information about the file that must be read.</param>
        /// <param name="dataSourceInfo">Optional information about the data source.</param>
        /// <returns>The entities that were read from the file.</returns>
        protected override IEnumerable <TEntity> ReadAllRecordsFromFile(FileInfo sourceFile, DataSourceInfo dataSourceInfo)
        {
            Encoding encoding = this.SourceFileEncoding;

            if (FileSourceInfo.IsSourceFileEncodingSpecified(dataSourceInfo))
            {
                encoding = FileSourceInfo.SelectSourceFileEncoding(dataSourceInfo);
            }

            return(this.serializer.Deserialize(sourceFile.FullName, encoding));
        }
Example #4
0
        /// <summary>Writes the specified records to the destination file.</summary>
        /// <param name="destinationFile">Information about the file in which the contents must be saved.</param>
        /// <param name="dataSourceInfo">Optional information about the data source.</param>
        /// <param name="contents">The new contents of the file.</param>
        /// <returns>The entities after they have been written to the file (in case the saving resulted in some updated values).</returns>
        protected override IEnumerable <TEntity> WriteAllRecordsToFile(FileInfo destinationFile, DataSourceInfo dataSourceInfo,
                                                                       IEnumerable <TEntity> contents)
        {
            Encoding encoding = this.SourceFileEncoding;

            if (FileSourceInfo.IsSourceFileEncodingSpecified(dataSourceInfo))
            {
                encoding = FileSourceInfo.SelectSourceFileEncoding(dataSourceInfo);
            }

            this.serializer.Serialize(contents, destinationFile.FullName, encoding);
            return(contents);
        }