www.pudn.com > Fetion.rar > ImportContactsFromCSV.cs


namespace Imps.Client.Pc 
{ 
    using Imps.Common; 
    using System; 
    using System.Collections.Generic; 
    using System.IO; 
    using System.Text; 
    using System.Windows.Forms; 
    using System.Xml; 
 
    internal class ImportContactsFromCSV : IImportContacts 
    { 
        private string _split = ","; 
 
        public string GetContactsXML(IWin32Window owner, string filePath) 
        { 
            StringWriter w = new StringWriter(); 
            XmlTextWriter writer2 = new XmlTextWriter(w); 
            writer2.Formatting = Formatting.None; 
            writer2.WriteStartElement("Contacts"); 
            using (StreamReader reader = new StreamReader(filePath, Encoding.Default, true)) 
            { 
                string line = reader.ReadLine(); 
                if (line.Contains("\t")) 
                { 
                    this._split = "\t"; 
                } 
                else 
                { 
                    this._split = ","; 
                } 
                List list = this.SplitCSVLine(line); 
                while (reader.Peek() >= 0) 
                { 
                    line = reader.ReadLine(); 
                    writer2.WriteStartElement("Contact"); 
                    List list2 = this.SplitCSVLine(line); 
                    for (int i = 0; i < list.Count; i++) 
                    { 
                        string str2; 
                        if (list2.Count > i) 
                        { 
                            str2 = list2[i]; 
                        } 
                        else 
                        { 
                            str2 = ""; 
                        } 
                        writer2.WriteElementString(list[i], str2); 
                    } 
                    writer2.WriteEndElement(); 
                } 
            } 
            writer2.WriteEndElement(); 
            return w.ToString(); 
        } 
 
        private bool IsEnd(string content, int index, bool startIsComma) 
        { 
            int num = 0; 
            while ((content.Length > (index + 1)) && (content.Substring(index, 1) == "\"")) 
            { 
                num++; 
                index++; 
            } 
            return (((num == 2) && (content.Substring(index, 1) == this._split)) || (((num == 0) && !startIsComma) || ((num % 2) != 0))); 
        } 
 
        private List SplitCSVLine(string line) 
        { 
            int num2; 
            List list = new List(); 
            string item = ""; 
            int startIndex = 0; 
            bool startIsComma = line.StartsWith("\""); 
            while ((num2 = line.IndexOf(this._split, startIndex)) >= 0) 
            { 
                item = item + line.Substring(startIndex, num2 - startIndex); 
                if (this.IsEnd(line, num2 + 1, startIsComma)) 
                { 
                    if (startIsComma) 
                    { 
                        list.Add(item.Remove(item.Length - 1, 1).Remove(0, 1)); 
                    } 
                    else 
                    { 
                        list.Add(item); 
                    } 
                    item = ""; 
                } 
                else 
                { 
                    item = item + this._split; 
                } 
                startIndex = num2 + 1; 
                if (line.Length > startIndex) 
                { 
                    startIsComma = line.Substring(startIndex, 1) == "\""; 
                } 
            } 
            if (line.Length > startIndex) 
            { 
                if (!startIsComma) 
                { 
                    list.Add(line.Substring(startIndex)); 
                    return list; 
                } 
                list.Add(line.Substring(startIndex + 1, (line.Length - startIndex) - 2)); 
            } 
            return list; 
        } 
 
        public string Filter 
        { 
            get 
            { 
                return "csv files (*.csv)|*.csv"; 
            } 
        } 
 
        public ImportContactType Type 
        { 
            get 
            { 
                return ImportContactType.CSV; 
            } 
        } 
    } 
}