Beispiel #1
0
        public Task loadTask()
        {
            string     taskFilePath = string.Empty;
            FileStream stream       = null;

            try
            {
                taskFilePath = getNextTaskFile();
                if (taskFilePath == string.Empty)
                {
                    return(null);
                }

                //XmlSerializer serializer = new XmlSerializer(typeof(TaskXmlNode));
                //stream = new FileStream(taskFilePath, FileMode.Open);
                //TaskXmlNode taskXmlNode = serializer.Deserialize(stream) as TaskXmlNode;
                //stream.Close();

                TaskXmlNode taskXmlNode = XmlDeserializer.deserialize <TaskXmlNode>(taskFilePath);

                Task task = new Task();
                task.ReportName = taskXmlNode.ReportSetting.ReportName;
                taskXmlNode.ReportSetting.ReportFilePath = Application.StartupPath + @"\Report" + taskXmlNode.ReportSetting.ReportFilePath;
                task.ReportSetting = taskXmlNode.ReportSetting;

                foreach (DataSourceXmlNode dataSourceXmlNode in taskXmlNode.DataSourceList)
                {
                    List <object> dataSourceValue = new List <object>();
                    string        prefix          = Application.StartupPath + @"\Assembly";
                    Type          type            = ReflectionHelper.getType(prefix + dataSourceXmlNode.AssemblyPath, dataSourceXmlNode.TypeFullName);

                    foreach (RecordXmlNode recordXmlNode in dataSourceXmlNode.RecordList)
                    {
                        Object obj = createEntity(type);
                        initEntity(type, obj, recordXmlNode.PropertyList);
                        dataSourceValue.Add(obj);
                    }
                    string     dataSourceName = generateDataSourceName(dataSourceXmlNode.TypeFullName);
                    DataSource dataSource     = new DataSource(dataSourceName, dataSourceValue);
                    task.addDataSource(dataSource);
                }

                task.CurrentFilePath = handleProcessedTask(taskFilePath);
                return(task);
            }
            catch (Exception ex)
            {
                if (stream != null)
                {
                    stream.Close();
                }

                if (File.Exists(taskFilePath))
                {
                    FileInfo info         = new FileInfo(taskFilePath);
                    string   dir          = GlobalVariable.getDeletedDirectory();
                    string   destFilePath = dir + @"\" + info.Name;
                    if (File.Exists(destFilePath))
                    {
                        File.Delete(destFilePath);
                    }
                    File.Move(taskFilePath, destFilePath);
                    throw new Exception(ex.Message + string.Format(" Failed to load task. Task file '{0}' has been moved to directory '{1}'.", new FileInfo(taskFilePath).Name, dir));
                }
                else
                {
                    throw new Exception(ex.Message + " Failed to load task.");
                }
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
        }
Beispiel #2
0
        public string toXml(string reportName, string rdlcName)
        {
            XmlWriter     writer     = null;
            Stream        stream     = null;
            XmlSerializer serializer = null;

            try
            {
                if (!Directory.Exists(Application.StartupPath + @"/PrintSetting/ReportSetting"))
                {
                    Directory.CreateDirectory(Application.StartupPath + @"/PrintSetting/ReportSetting");
                }

                string[]      fileList = Directory.GetFiles(Application.StartupPath + @"/PrintSetting/ReportSetting");
                bool          hasCustomizedPrintSetting = false;
                ReportSetting reportSetting             = null;

                for (int i = 0; i < fileList.Length; ++i)
                {
                    try
                    {
                        serializer    = new XmlSerializer(typeof(ReportSetting));
                        stream        = File.Open(fileList[i], FileMode.Open, FileAccess.Read);
                        reportSetting = serializer.Deserialize(stream) as ReportSetting;
                        if (reportSetting.ReportName == reportName)
                        {
                            hasCustomizedPrintSetting = true;
                            break;
                        }
                    }
                    catch
                    {
                        throw new Exception(string.Format("读取自定义打印配置文件'[0]'时发生错误。", fileList[i]));
                    }
                }

                if (!hasCustomizedPrintSetting)
                {
                    PrintSetting printSetting = null;
                    if (!File.Exists(Application.StartupPath + @"/PrintSetting/DefaultPrintSetting.xml"))
                    {
                        throw new Exception("未配置打印机。");
                    }

                    try
                    {
                        serializer   = new XmlSerializer(typeof(PrintSetting));
                        stream       = File.Open(Application.StartupPath + @"/PrintSetting/DefaultPrintSetting.xml", FileMode.Open, FileAccess.Read);
                        printSetting = serializer.Deserialize(stream) as PrintSetting;
                    }
                    catch
                    {
                        throw new Exception(string.Format("读取默认打印配置文件'[0]'时发生错误。", Application.StartupPath + @"/PrintSetting/DefaultPrintSetting.xml"));
                    }

                    reportSetting              = new ReportSetting();
                    reportSetting.ReportName   = reportName;
                    reportSetting.PrintSetting = printSetting;
                }

                TaskXmlNode taskXmlNode = new TaskXmlNode();

                FileInfo info            = new FileInfo(Application.ExecutablePath);
                string   applicationName = info.Name.Substring(0, info.Name.LastIndexOf('.'));
                reportSetting.ReportFilePath = string.Format(@"\{0}\{1}", applicationName, rdlcName);
                taskXmlNode.ReportSetting    = reportSetting;


                foreach (IList <object> objList in this.list)
                {
                    if (objList.Count == 0)
                    {
                        continue;
                    }

                    DataSourceXmlNode dataSourceXmlNode = new DataSourceXmlNode();
                    taskXmlNode.DataSourceList.Add(dataSourceXmlNode);

                    Type type = objList[0].GetType();
                    dataSourceXmlNode.AssemblyPath = string.Format(@"\{0}\{1}", applicationName, new FileInfo(type.Assembly.Location).Name);
                    dataSourceXmlNode.TypeFullName = type.FullName;

                    foreach (object obj in objList)
                    {
                        RecordXmlNode recordXmlNode = new RecordXmlNode();
                        dataSourceXmlNode.RecordList.Add(recordXmlNode);

                        List <PropertyXmlNode> propList = readProperty(obj, type);

                        foreach (PropertyXmlNode prop in propList)
                        {
                            recordXmlNode.PropertyList.Add(prop);
                        }
                    }
                }

                serializer = new XmlSerializer(typeof(TaskXmlNode));
                StringBuilder builder = new StringBuilder();
                writer = XmlWriter.Create(builder);
                serializer.Serialize(writer, taskXmlNode);
                return(builder.ToString().Replace("utf-16", "utf-8"));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message + " 无法生成打印所需的XML文件。");
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
                if (stream != null)
                {
                    stream.Close();
                }
            }
        }