代码

XML序列化工具类

 public static class XMLHelper
{
/// <summary>
/// 将对象序列化为指定的文件名
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public static bool SaveObjAsXml<T>(T obj,string fileName)
{ var dir = Application.StartupPath;
try
{
FileStream fs = new FileStream(dir+"/"+fileName, FileMode.Create);
XmlSerializer xs = new XmlSerializer(typeof(T));
xs.Serialize(fs, obj);
fs.Close();
return true;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
} /// <summary>
/// 将xml文件进行反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="fileName"></param>
/// <returns></returns>
public static T DecodeXML<T>(string fileName)
{
var dir = Application.StartupPath;
fileName = dir + "/" + fileName;
try
{
if (File.Exists(fileName)==false)
return default(T);
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
XmlSerializer xs = new XmlSerializer(typeof(T));
T obj = (T)xs.Deserialize(fs);
return obj;
}catch(Exception ex)
{
Console.WriteLine(ex.Message);
throw;
} }
}

对应的窗体数据类可以定义为:

[XmlRoot]
public class WindowsData:PropertyChangedBase
{
[XmlElement("WindowsIndex")]
/// <summary>
/// Windows窗体索引
/// </summary>
public int WindowsIndex{get;set; } [XmlElement("FontSize")]
private double fontSize=14;
/// <summary>
/// 窗体字体
/// </summary>
public double FontSize
{
get { return fontSize;}
set
{
fontSize=value;
OnPropertyChanged("FontSize");
}
} private Point startUpPosition;
[XmlElement("LeftTopWinPosition")]
public Point StartUpPosition
{
get { return startUpPosition; }
set
{
startUpPosition = value; OnPropertyChanged("StartUpPosition");
}
} /// <summary>
/// 窗体宽度
/// </summary>
private int windowsWidth;
public int WindowsWidth
{
get { return windowsWidth; }
set { windowsWidth = value;OnPropertyChanged("WindowsWidth"); }
} private int windowsHeight;
public int WindowsHeight
{
get { return windowsHeight; }
set
{
windowsHeight = value;
OnPropertyChanged("WindowsHeight");
}
} private string richTextBoxContent; [XmlElement("UserInputNotes")]
public string RichTextBoxContent
{
get { return richTextBoxContent; }
set
{
richTextBoxContent = value;
OnPropertyChanged("RichTextBoxContent");
}
}
public WindowsData()
{
// MessageBox.Show("新的窗体数据加载了");
WindowsIndex = GenerateWindowsIndex.Generate();
WindowsWidth = 350;
WindowsHeight = 450;
double screenHeight = SystemParameters.FullPrimaryScreenHeight;
double screenWidth = SystemParameters.FullPrimaryScreenWidth;
StartUpPosition =new Point((screenWidth - WindowsWidth)/2,(screenHeight - WindowsHeight)/2);
}
}

最新文章

  1. mybatis配置-返回date类型丢失时间
  2. 江豚科技|专业移动APP开发与移动互联网解决方案
  3. 分享一个导航条哈(⊙o⊙)…
  4. 【BZOJ】3669: [Noi2014]魔法森林(lct+特殊的技巧)
  5. wrap device
  6. 获取oracle 表字段,表名,以及主键之类等等的信息
  7. C++自定义异常处理
  8. mybatis中association的column传入多个参数值
  9. 论文:network embedding
  10. effective c++ 条款13 use object to manage resources.
  11. SQL中条件放在on后与where后的区别
  12. dev repositoryItem 手工定义
  13. win 10 开机后无法显示桌面
  14. 六、web应用与Tomcat
  15. 吴恩达机器学习笔记38-决策下一步做什么(Deciding What to Do Next Revisited)
  16. 读取磁盘:CHS方式
  17. jquery弹窗时禁止body滚动条滚动
  18. Codeforces Round #395 (Div. 2)
  19. windows linux hosts文件的配置,开发项目中域名跳转等。
  20. 从零开始学Kotlin-基础语法(1)

热门文章

  1. Aspose.Words 操作 Word 画 EChart 图
  2. 齐博x1内容页中下一页上一页的标签
  3. 这次彻底读透 Redis
  4. 人生苦短,我用 python 之入门篇
  5. Debian 参考手册之第6章Debian档案库
  6. Python基础部分:4、 python语法之注释
  7. Jmeter之聚合报告“造假”
  8. IO学习笔记
  9. 不借助idea开发工具构建一个Javaweb项目
  10. 2022春每日一题:Day 30