Пример #1
0
        public void SaveDelimitedFile(string rootNodePath, string filename, Encoding encoding = null, bool includeHeader = true)
        {
            using (var dest = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite))
            {
                using (var writer = new FlatWriter(dest, encoding ?? _defaultEncoding))
                {
                    writer.AllowQuotes   = _allowQuotes;
                    writer.AlwaysQuote   = _alwaysQuote;
                    writer.IncludeHeader = includeHeader;

                    var rootNode = _nodes.GetNode(rootNodePath);
                    if (_columns.Count == 0)
                    {
                        _columns = rootNode.Members.Where(x => x.Value.IsSingleValueNode).Select(x => x.Value.Name).ToList();
                    }

                    var rows = _rows.GetRows(rootNode).ToList();

                    writer.WriteDelimited(rows, SharpMapType.Variable, _delimiter, _columns, _aliases);
                }
            }
        }
Пример #2
0
        public void SaveFlatFile(string rootNodePath, string filename, string formatfile = null, Encoding encoding = null)
        {
            if (formatfile == null)
            {
                var fi = new FileInfo(filename);
                formatfile = filename.Substring(0, filename.Length - fi.Extension.Length) + ".fmt";
            }

            var rows = _rows.GetRows();

            SharpNodeMap nodeMap;

            if (!_mapSets.TryGetValue(rootNodePath, out nodeMap))
            {
                var rootNode = _nodes.GetNode(rootNodePath);
                var maps     = _nodes.GetNodes().Where(n => n is SharpNodeMap && n.Parent == rootNode).ToList();
                if (maps.Count > 1)
                {
                    throw new Exception(string.Format("Could not save flat file format: Multiple column mappings found for '{0}'", rootNodePath));
                }

                nodeMap = maps.FirstOrDefault() as SharpNodeMap;
            }

            if (nodeMap == null)
            {
                throw new Exception(string.Format("Could not save flat file format: No column mapping not found for '{0}'", rootNodePath));
            }

            FormatWriter.SaveFormatFile(formatfile, nodeMap, encoding ?? _defaultEncoding);

            using (var dest = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite))
            {
                using (var writer = new FlatWriter(dest, encoding ?? _defaultEncoding))
                {
                    writer.Write(nodeMap, rows);
                }
            }
        }