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


namespace Imps.Client.Pc 
{ 
    using System; 
    using System.IO; 
    using System.Text; 
    using System.Windows.Forms; 
    using System.Xml; 
 
    internal class ExportContactsToCVS : IExportContacts 
    { 
        public void ExportContacts(IWin32Window owner, string filePath, string xmlContacts) 
        { 
            XmlDocument document = new XmlDocument(); 
            document.LoadXml(xmlContacts); 
            if (document.DocumentElement.ChildNodes.Count != 0) 
            { 
                StringBuilder builder = new StringBuilder(); 
                for (int i = 0; i < document.DocumentElement.ChildNodes[0].ChildNodes.Count; i++) 
                { 
                    builder.Append(string.Format("\"{0}\"", document.DocumentElement.ChildNodes[0].ChildNodes[i].Name)); 
                    if (i != (document.DocumentElement.ChildNodes[0].ChildNodes.Count - 1)) 
                    { 
                        builder.Append(","); 
                    } 
                    else 
                    { 
                        builder.Append("\r\n"); 
                    } 
                } 
                foreach (XmlNode node in document.DocumentElement.ChildNodes) 
                { 
                    for (int j = 0; j < node.ChildNodes.Count; j++) 
                    { 
                        builder.Append(string.Format("\"{0}\"", node.ChildNodes[j].InnerText)); 
                        if ((node.ChildNodes.Count - 1) != j) 
                        { 
                            builder.Append(","); 
                        } 
                        else 
                        { 
                            builder.Append("\r\n"); 
                        } 
                    } 
                } 
                using (StreamWriter writer = new StreamWriter(filePath, false, ImportExportManager.FileEncoding)) 
                { 
                    writer.Write(builder.ToString()); 
                } 
            } 
        } 
 
        public string Filter 
        { 
            get 
            { 
                return "csv files (*.csv)|*.csv"; 
            } 
        } 
    } 
}