public override void ExportToCsv(string xmlTableName, string csvDestinationFilePath, Encoding encoding)
        {
            _csvDestinationFilePath = csvDestinationFilePath;

            HeaderColumnNameCollection.Clear();

            _csvDestinationFilePath = csvDestinationFilePath;

            using (XmlReader reader = XmlReader.Create(_xmlSourceFilePath))
            {
                IEnumerable <XElement> workingTable =
                    from el in reader.StreamElements(xmlTableName).DescendantsAndSelf()
                    where el.Descendants().Count() > 0
                    select el;

                IEnumerable <XElement> list = workingTable.ToList();

                var fs = new FileStream(_csvDestinationFilePath, FileMode.Create, FileAccess.Write, FileShare.None);
                var sw = new StreamWriter(fs, encoding);

                string headerLine = string.Empty;

                foreach (XElement x in list.Take(1).Descendants())
                {
                    HeaderColumnNameCollection.Add(ColumnCount, x.Name.ToString());
                    headerLine += x.Name + ",";
                    ColumnCount++;
                }

                using (sw)
                {
                    char[] charsToTrim = { ',' };
                    sw.WriteLine(headerLine.TrimEnd(charsToTrim));

                    foreach (XElement element in list)
                    {
                        string rowString    = string.Empty;
                        string columnString = string.Empty;

                        foreach (var obj in element.Descendants())
                        {
                            columnString += obj.Value + ",";
                        }

                        rowString += columnString;
                        rowString  = rowString.Replace(Environment.NewLine, @"-");
                        sw.WriteLine(rowString.TrimEnd(charsToTrim));
                    }

                    sw.Close();
                }

                reader.Close();
            }
        }
Exemple #2
0
        private StreamWriter CreateStreamWriter(string xmlTableName, string csvDestinationFilePath, Encoding encoding)
        {
            if (string.IsNullOrEmpty(xmlTableName))
            {
                throw new NotSupportedException("Table name for table to export is not specified");
            }

            HeaderColumnNameCollection.Clear();

            _csvDestinationFilePath = csvDestinationFilePath;
            _workingTable           = XmlDataSet.Tables[xmlTableName];
            ColumnCount             = _workingTable.Columns.Count;

            foreach (DataColumn column in _workingTable.Columns)
            {
                HeaderColumnNameCollection.Add(column.Ordinal, column.ColumnName);
            }

            var fs = new FileStream(_csvDestinationFilePath, FileMode.Create, FileAccess.Write, FileShare.None);
            var sw = new StreamWriter(fs, encoding);

            return(sw);
        }