コード例 #1
0
        /// <summary>
        /// 输出多個 XML files,每個 XML file 最多只包含 2 萬笔资料
        /// </summary>
        /// <param name="xmlFilePath">要输出的 XML file 路径</param>
        /// <returns>成功输出的 XML file path list</returns>
        private List <string> ExportMultiple4BytesAlignXmls(
            string xmlOutputFolderPath,
            AddrList.DevDataTable Devices)
        {
            const int     SEGMENT_SIZE = 20000;
            List <string> files        = new List <string>();

            if (Devices.Count() > 0)
            {
                int fileCount = (Devices.Count() - 1) / SEGMENT_SIZE + 1;
                int devIndex  = 0;
                for (int i = 0; i < fileCount; ++i)
                {
                    string xmlFilePath = (i == 0)
                        ? (xmlOutputFolderPath + @"\addressbook.xml")
                        : (xmlOutputFolderPath + @"\addressbook" + i + ".xml");

                    AddrList ds       = new AddrList();
                    int      devCount = Math.Min(SEGMENT_SIZE, Devices.Count() - i * SEGMENT_SIZE);
                    for (int j = 0; j < devCount; ++j)
                    {
                        ds.dev.ImportRow(Devices[devIndex++]);
                    }
                    if (ExportXml(xmlFilePath, ds.dev) && Export4BytesAlignXml(xmlFilePath, xmlFilePath))
                    {
                        files.Add(xmlFilePath);
                    }
                }
            }
            return(files);
        }
コード例 #2
0
        private void ExportAddressBookUclPkg(string pkgFilePath, AddrList.DevDataTable Devices)
        {
            List <string> xmlFilesPath = ExportMultiple4BytesAlignXmls(
                Path.GetAddressBookTempFolderPath(),
                Devices);
            List <string> uclFilesPath      = new List <string>();
            string        uclTempFolderPath = Path.GetAddressBookUclTempFolderPath();
            int           count             = xmlFilesPath.Count;

            for (int i = 0; i < count; ++i)
            {
                string xmlFilePath = xmlFilesPath[i];
                string uclFilePath = (i == 0)
                ? (uclTempFolderPath + @"\addressbook.ucl")
                : (uclTempFolderPath + @"\addressbook" + i + ".ucl");

                try
                {
                    if (false == FileConverter.Xml2Ucl(xmlFilePath, uclFilePath))
                    {
                        throw new Exception("UCL无法導出");
                    }
                    uclFilesPath.Add(uclFilePath);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "操作错误",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            if (uclFilesPath.Count > 0)
            {
                PkgTool.ToPkgFile(uclTempFolderPath, pkgFilePath);
            }

            foreach (string filePath in xmlFilesPath)
            {
                File.Delete(filePath);
            }
            foreach (string filePath in uclFilesPath)
            {
                File.Delete(filePath);
            }
        }
コード例 #3
0
        private void ExportAddressBookXmlPkg(string pkgFilePath, AddrList.DevDataTable Devices)
        {
            string xmlFilePath = Path.GetAddressBookTempXmlFilePath();

            if (ExportXml(xmlFilePath, Devices))
            {
                try
                {
                    PkgTool.ToPkgFile(System.IO.Path.GetDirectoryName(xmlFilePath), pkgFilePath);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    File.Delete(xmlFilePath);
                }
            }
        }
コード例 #4
0
        private bool ExportXml(string xmlFilePath, AddrList.DevDataTable Devices)
        {
            bool result = false;
            XmlWriterSettings settings = new XmlWriterSettings
            {
                Indent = true
            };

            using (MemoryStream stream = new MemoryStream())
            {
                using (XmlWriter writer = XmlWriter.Create(stream, settings))
                {
                    try
                    {
                        Devices.WriteXml(writer);
                        // Reset stream to origin
                        stream.Seek(0, SeekOrigin.Begin);
                        // Load stream as XDocument
                        XDocument xdoc = XDocument.Load(stream);
                        // TODO 版本号应该要可以修改
                        xdoc.Element("AddrList").SetAttributeValue("ver", "1.0");
                        // Save to file as XML
                        xdoc.Save(xmlFilePath);
                        result = true;
                    }
                    catch (Exception)
                    {
                        //MessageBox.Show(ex.Message, "操作错误",
                        //                MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    finally
                    {
                        writer.Close();
                    }
                }
            }
            return(result);
        }