[WPF]xml序列化以及反序列化数据
代码#
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);
}
}
作者:lizhenghao126
出处:https://www.cnblogs.com/lizhenghao126/p/11053590.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
https://github.com/li-zheng-hao
分类:
WPF
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)