private static GeocodeFeed ParseDelimitedFile(StreamReader textReader, BatchFileFormat format)
        {
            char delimiter;

            switch (format)
            {
            case BatchFileFormat.PIPE:
                delimiter = '|';
                break;

            case BatchFileFormat.TAB:
                delimiter = '\t';
                break;

            case BatchFileFormat.CSV:
            default:
                delimiter = ',';
                break;
            }

            var    feed          = new GeocodeFeed();
            double schemaVersion = 1.0;

            using (var reader = new DelimitedFileReader(textReader, delimiter))
            {
                var row = reader.GetNextRow();

                if (row != null && row.Count > 0)
                {
                    //Parse Schema Version info.
                    if (row[0].StartsWith("Bing Spatial Data Services", StringComparison.OrdinalIgnoreCase) && row.Count >= 2)
                    {
                        double.TryParse(row[1], out schemaVersion);
                        row = reader.GetNextRow();
                    }

                    //Skip header
                    if (string.Compare(row[0], "Id", StringComparison.OrdinalIgnoreCase) == 0 ||
                        string.Compare(row[0], "GeocodeEntity/@Id", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        row = reader.GetNextRow();
                    }

                    //Parse rows
                    while (row != null && row.Count > 0)
                    {
                        var parsedRow = ParseRow(row, schemaVersion);

                        if (parsedRow != null)
                        {
                            feed.Entities.Add(parsedRow);
                        }

                        row = reader.GetNextRow();
                    }
                }
            }

            return(feed);
        }
        /// <summary>
        /// Deserializes a GeocodeFeed from a file stream.
        /// </summary>
        /// <param name="stream">GeocodeFeed a file stream.</param>
        /// <param name="fileFormat">The batch file format of the stream.</param>
        /// <returns>A GeocodeFeed that has been parsed from a stream.</returns>
        public static Task <GeocodeFeed> ReadAsync(Stream stream, BatchFileFormat fileFormat)
        {
            if (fileFormat == BatchFileFormat.XML)
            {
                return(ReadAsync(stream));
            }
            else
            {
                return(Task.Run <GeocodeFeed>(() =>
                {
                    var ms = new MemoryStream();

                    try
                    {
                        stream.CopyTo(ms);
                        if (XmlUtilities.IsStreamCompressed(ms))
                        {
                            //Uncompress the first file in the zipped stream.
                            var zipArchive = new System.IO.Compression.ZipArchive(ms);
                            if (zipArchive.Entries != null && zipArchive.Entries.Count > 0)
                            {
                                stream = zipArchive.Entries[0].Open();

                                ms = new MemoryStream();
                                stream.CopyTo(ms);
                            }
                        }

                        ms.Position = 0;

                        using (var reader = new StreamReader(ms))
                        {
                            return ParseDelimitedFile(reader, fileFormat);
                        }
                    }
                    catch { }

                    ms.Dispose();
                    return null;
                }));
            }
        }
Beispiel #3
0
        /// <summary>
        /// Give a batch geocoding file format, this method provides a default and file extension filter string.
        /// </summary>
        /// <param name="format">The batch geocoding file file format to get the extensions for.</param>
        /// <param name="defaultExt">Default file extension.</param>
        /// <param name="filters">A file extension filter string</param>
        public static void GetFileExtensions(BatchFileFormat format, out string defaultExt, out string filters)
        {
            switch (format)
            {
            case BatchFileFormat.CSV:
                defaultExt = ".csv";
                filters    = "CSV (*.csv)|*.csv|Text (*.txt)|*.txt|Zip (*.zip)|*.zip|All files (*.*)|*.*";
                break;

            case BatchFileFormat.TAB:
            case BatchFileFormat.PIPE:
                defaultExt = ".txt";
                filters    = "Text (*.txt)|*.txt|Zip (*.zip)|*.zip|All files (*.*)|*.*";
                break;

            case BatchFileFormat.XML:
            default:
                defaultExt = ".xml";
                filters    = "Xml (.xml)|*.xml|Zip (*.zip)|*.zip|All files (*.*)|*.*";
                break;
            }
        }