/// <summary> /// 导出. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnExp_Click(object sender, EventArgs e) { if (this.saveFileDialog1.ShowDialog() != System.Windows.Forms.DialogResult.OK) { // 用户取消. return; } // 取得代码. string chapterCode = this.cboChapters.SelectedValue as string; if (String.IsNullOrEmpty(chapterCode)) { MyMessage.Warn("未选择章节!"); return; } Chapter dbChapter = null; using (MyTranslateContext context = new MyTranslateContext()) { dbChapter = context.Chapters.Include("Book").Include("Lines").FirstOrDefault(p => p.ChapterCode == chapterCode); } if (dbChapter == null) { MyMessage.Warn("章节数据不存在!"); return; } Chapter expChapter = new Chapter(); CommonModelCopyer.ModelCopy(dbChapter, expChapter); expChapter.Lines = new List <Line>(); foreach (Line dbLine in dbChapter.Lines) { Line expLine = new Line(); CommonModelCopyer.ModelCopy(dbLine, expLine); expLine.Status = dbLine.Status; expChapter.Lines.Add(expLine); } // 输出 UTF-8 的 XML 文件. XmlSerializer xs = new XmlSerializer(typeof(Chapter)); using (StreamWriter sw = new StreamWriter(this.saveFileDialog1.FileName)) { xs.Serialize(sw, expChapter); } MyMessage.Success("导出完毕!"); }
static void Main(string[] args) { Console.WriteLine("测试使用 反射方式, 在两个对象之间复制属性!"); // 构造源对象. TestModelFrom testFrom = new TestModelFrom() { TestNormal = "正常属性", TestFromOnly = "源 特有属性", TestFromWriteOnly = "源只写属性", TestToReadOnly = "目标只读属性", TestToWriteOnly = "目标只写属性", }; Console.WriteLine("源对象:{0}", testFrom); // 目标对象. TestModelTo testTo = new TestModelTo(); Console.WriteLine("复制前 目标对象:{0}", testTo); CommonModelCopyer.ModelCopy(testFrom, testTo); Console.WriteLine("复制后 目标对象:{0}", testTo); Console.ReadLine(); }
/// <summary> /// 导出 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnExp_Click(object sender, EventArgs e) { if (this.mainDataList == null || this.mainDataList.Count == 0) { MyMessage.Warn("当前没有数据可导出!"); return; } if (this.saveFileDialog1.ShowDialog() != System.Windows.Forms.DialogResult.OK) { // 用户取消. return; } List <AutoReplace> todoList = new List <AutoReplace>(); foreach (var item in this.mainDataList) { AutoReplace expData = new AutoReplace(); CommonModelCopyer.ModelCopy(item, expData); expData.Status = item.Status; todoList.Add(expData); } // 输出 UTF-8 的 XML 文件. XmlSerializer xs = new XmlSerializer(typeof(List <AutoReplace>)); using (StreamWriter sw = new StreamWriter(this.saveFileDialog1.FileName)) { xs.Serialize(sw, todoList); } MyMessage.Success("导出完毕!"); }
/// <summary> /// 导出书籍. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnExpBook_Click(object sender, EventArgs e) { // 取得代码. string bookCode = this.cboBooks.SelectedValue as string; if (String.IsNullOrEmpty(bookCode)) { MyMessage.Warn("未选择书籍!"); return; } if (this.saveFileDialog1.ShowDialog() != System.Windows.Forms.DialogResult.OK) { // 用户取消. return; } try { this.Cursor = Cursors.WaitCursor; this.gvLines.Enabled = false; Book dbBook = null; Book expBook = new Book(); using (MyTranslateContext context = new MyTranslateContext()) { dbBook = context.Books.Include("Chapters").FirstOrDefault(p => p.BookCode == bookCode); if (dbBook == null) { MyMessage.Warn("书籍数据不存在!"); return; } CommonModelCopyer.ModelCopy(dbBook, expBook); expBook.Chapters = new List <Chapter>(); foreach (Chapter dbChapter in dbBook.Chapters) { Chapter expChapter = new Chapter(); CommonModelCopyer.ModelCopy(dbChapter, expChapter); expChapter.Status = dbChapter.Status; expChapter.Lines = new List <Line>(); foreach (Line dbLine in dbChapter.Lines) { Line expLine = new Line(); CommonModelCopyer.ModelCopy(dbLine, expLine); expLine.Status = dbLine.Status; expChapter.Lines.Add(expLine); } expBook.Chapters.Add(expChapter); } } // 输出 UTF-8 的 XML 文件. XmlSerializer xs = new XmlSerializer(typeof(Book)); using (StreamWriter sw = new StreamWriter(this.saveFileDialog1.FileName)) { xs.Serialize(sw, expBook); } MyMessage.Success("导出完毕!"); } catch (Exception ex) { MyMessage.Fail(ex.Message); } finally { this.Cursor = Cursors.Default; this.gvLines.Enabled = true; } }