当前位置: 首页 > news >正文

工程建设的招标在哪个招标网站网站模板建站公司

工程建设的招标在哪个招标网站,网站模板建站公司,景县网站建设,wordpress火吗场景 Winform中实现序列化指定类型的对象到指定的Xml文件和从指定的Xml文件中反序列化指定类型的对象: Winform中实现序列化指定类型的对象到指定的Xml文件和从指定的Xml文件中反序列化指定类型的对象_winform xml序列化_霸道流氓气质的博客-CSDN博客 上面讲的序…

场景

Winform中实现序列化指定类型的对象到指定的Xml文件和从指定的Xml文件中反序列化指定类型的对象:

Winform中实现序列化指定类型的对象到指定的Xml文件和从指定的Xml文件中反序列化指定类型的对象_winform xml序列化_霸道流氓气质的博客-CSDN博客

上面讲的序列化对象的流程需要进行补充。

Winform程序需要将某些动态配置的TextBox的内容在配置后进行保存,下次启动时仍然读取加载显示之前的配置。

注:

博客:
霸道流氓气质的博客_CSDN博客-C#,架构之路,SpringBoot领域博主

实现

1、新建配置项对象类,属性根据要保存的内容对应而定。

    [Serializable]public class DataConfig{/// <summary>/// 煤矿编码/// </summary>public string MineCode { get; set; }/// <summary>/// 煤业公司编码/// </summary>public string CompanyCode { get; set; }/// <summary>/// http地址/// </summary>public string HttpAddress { get; set; }/// <summary>/// MqttIp/// </summary>public string MqttIp { get; set; }/// <summary>/// MqttPort/// </summary>public string MqttPort { get; set; }/// <summary>/// MqttUsername/// </summary>public string MqttUsername { get; set; }/// <summary>/// MqttPassword/// </summary>public string MqttPassword { get; set; }}

注意要在类上添加

[Serializable]

2、然后新建序列化对象的工具类SerializeXmlHelper

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;namespace DataConvert.dataconvert
{class SerializeXmlHelper{/// <summary>/// 序列化指定类型的对象到指定的Xml文件/// </summary>/// <typeparam name="T">要序列化的对象类型</typeparam>/// <param name="obj">要序列化的对象</param>/// <param name="xmlFileName">保存对象数据的完整文件名</param>public static void SerializeXml<T>(T obj, string xmlFileName){lock (xmlFileName){try{string dir = Path.GetDirectoryName(xmlFileName);       //获取文件路径if (!Directory.Exists(dir)){Directory.CreateDirectory(dir);}string xmlContent = SerializeObject<T>(obj);FileHelper.WriteFile(xmlFileName, xmlContent, Encoding.UTF8);}catch (Exception ex){Console.Write(ex);}}}/// <summary>/// 把对象序列化为xml字符串/// </summary>/// <typeparam name="T"></typeparam>/// <param name="obj"></param>/// <returns></returns>public static string SerializeObject<T>(T obj){if (obj != null){StringWriter strWriter = new StringWriter();XmlSerializer serializer = new XmlSerializer(typeof(T));serializer.Serialize(strWriter, obj);return strWriter.ToString();}else{return String.Empty;}}/// <summary>/// 从指定的Xml文件中反序列化指定类型的对象/// </summary>/// <typeparam name="T">反序列化的对象类型</typeparam>/// <param name="xmlFileName">保存对象数据的文件名</param>/// <returns>返回反序列化出的对象实例</returns>public static T DeserializeXml<T>(string xmlFileName){lock (xmlFileName){try{if (!File.Exists(xmlFileName)){Console.Write("序列化文件不存在!");return default(T);}else{string xmlContent = FileHelper.ReadFile(xmlFileName, Encoding.UTF8);T obj = DeserializeObject<T>(xmlContent);return obj;}}catch (Exception ex){Console.Write(ex);return default(T);}}}/// <summary>/// 把xml字符串反序列化为对象/// </summary>/// <typeparam name="T"></typeparam>/// <param name="xmlString"></param>/// <returns></returns>public static T DeserializeObject<T>(string xmlString){if (!String.IsNullOrEmpty(xmlString)){StringReader strReader = new StringReader(xmlString);XmlSerializer serializer = new XmlSerializer(typeof(T));T obj = (T)serializer.Deserialize(strReader);return obj;}else{return default(T);}}/// <summary>/// 向指定文件写入内容/// </summary>/// <param name="path">要写入内容的文件完整路径</param>/// <param name="content">要写入的内容</param>/// <param name="encoding">编码格式</param>public static void WriteFile(string path, string content, System.Text.Encoding encoding){try{object obj = new object();if (!File.Exists(path)){FileStream fileStream = File.Create(path);fileStream.Close();}lock (obj){using (StreamWriter streamWriter = new StreamWriter(path, false, encoding)){streamWriter.WriteLine(content);streamWriter.Close();streamWriter.Dispose();}}}catch (Exception ex){Console.Write(ex);}}}
}

部分方法中用到了文件操作的相关工具类方法,所以新建文件操作工具类FileHelper

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace DataConvert.com.bdtd.dataconvert
{class FileHelper{/// <summary>/// 向指定文件写入内容/// </summary>/// <param name="path">要写入内容的文件完整路径</param>/// <param name="content">要写入的内容</param>public static void WriteFile(string path, string content){try{object obj = new object();if (!System.IO.File.Exists(path)){System.IO.FileStream fileStream = System.IO.File.Create(path);fileStream.Close();}lock (obj){using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(path, false, System.Text.Encoding.Default)){streamWriter.WriteLine(content);streamWriter.Close();streamWriter.Dispose();}}}catch (Exception ex){Console.WriteLine(ex.Message);}}/// <summary>/// 向指定文件写入内容/// </summary>/// <param name="path">要写入内容的文件完整路径</param>/// <param name="content">要写入的内容</param>/// <param name="encoding">编码格式</param>public static void WriteFile(string path, string content, System.Text.Encoding encoding){try{object obj = new object();if (!System.IO.File.Exists(path)){System.IO.FileStream fileStream = System.IO.File.Create(path);fileStream.Close();}lock (obj){using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(path, false, encoding)){streamWriter.WriteLine(content);streamWriter.Close();streamWriter.Dispose();}}}catch (Exception ex){Console.WriteLine(ex.Message);}}/// <summary>/// 读取文件内容/// </summary>/// <param name="path">要读取的文件路径</param>/// <param name="encoding">编码格式</param>/// <returns>返回文件内容</returns>public static string ReadFile(string path, System.Text.Encoding encoding){string result;if (!System.IO.File.Exists(path)){result = "不存在相应的目录";}else{System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, encoding);result = streamReader.ReadToEnd();streamReader.Close();streamReader.Dispose();}return result;}}
}

3、序列化对象到文件

这里可以在按钮的点击事件或者窗体的closing事件中获取到各textBox的内容并序列化到xml文件中

        private void button_save_config_Click(object sender, EventArgs e){//保存配置到文件try {saveConfigToFile();MessageBox.Show("配置保存成功");} catch (Exception ex) {MessageBox.Show("配置保存失败:"+ex.Message);}           }

具体执行的方法

        private void saveConfigToFile() {try {DataConfig dataConfig = new DataConfig();dataConfig.MineCode = textBox_mine_code.Text.Trim();dataConfig.CompanyCode = textBox_company_code.Text.Trim();dataConfig.HttpAddress = textBox_origin_address.Text.Trim();dataConfig.MqttIp = textBox_target_address.Text.Trim();dataConfig.MqttPort = textBox_mqtt_port.Text.Trim();dataConfig.MqttUsername = textBox_mqtt_username.Text.Trim();dataConfig.MqttPassword = textBox_mqtt_password.Text.Trim();string path = configFilePath;SerializeXmlHelper.SerializeXml(dataConfig, path);} catch(Exception ex) {textBox_log.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":保存配置到文件失败:" + ex.Message);textBox_log.AppendText("\r\n");}}

前面都是获取textBox中的输入内容,后面两行才是进行序列化对象的实现。

文件的路径提前声明变量

        //配置文件存放路径private string configFilePath = Application.StartupPath + "\\config\\config.xml";

这里将配置文件放在启动目录下的config下的config.xml中

4、反序列化配置文件到对象

可以在窗体的load时间中读取配置文件并反序列化到对象,然后给各控件赋值。

        private void Form1_Load(object sender, EventArgs e){//从配置文件读取配置readConfigFromFile();}

实现方法

        //从配置文件读取配置private void readConfigFromFile(){try {DataConfig dataConfig = new DataConfig();string path = configFilePath;if (File.Exists(path)) {dataConfig = SerializeXmlHelper.DeserializeXml<DataConfig>(path);textBox_mine_code.Text = dataConfig.MineCode;textBox_company_code.Text = dataConfig.CompanyCode;textBox_origin_address.Text = dataConfig.HttpAddress;textBox_target_address.Text = dataConfig.MqttIp;textBox_mqtt_port.Text = dataConfig.MqttPort;textBox_mqtt_username.Text = dataConfig.MqttUsername;textBox_mqtt_password.Text = dataConfig.MqttPassword;}} catch(Exception ex) {textBox_log.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":从配置文件读取配置失败:" + ex.Message);textBox_log.AppendText("\r\n");}}

注意这里要规避首次加载没有配置文件或者人为删除配置文件的情况,需要加判断。

其次上面在新建xml文件中,也对config路径做了判断,如果不存在则创建。


文章转载自:
http://revivalism.jqLx.cn
http://terpsichore.jqLx.cn
http://euchre.jqLx.cn
http://multichain.jqLx.cn
http://inductively.jqLx.cn
http://furbearer.jqLx.cn
http://symbolatry.jqLx.cn
http://candleholder.jqLx.cn
http://enforcement.jqLx.cn
http://ascensionist.jqLx.cn
http://unrighteousness.jqLx.cn
http://fireproof.jqLx.cn
http://cheap.jqLx.cn
http://vahana.jqLx.cn
http://woebegone.jqLx.cn
http://dedicator.jqLx.cn
http://neoclassic.jqLx.cn
http://labour.jqLx.cn
http://magnetostatics.jqLx.cn
http://hermaphroditic.jqLx.cn
http://crux.jqLx.cn
http://poikilitic.jqLx.cn
http://nonallergenic.jqLx.cn
http://stoneman.jqLx.cn
http://diastereoisomer.jqLx.cn
http://carcinogenic.jqLx.cn
http://conflicting.jqLx.cn
http://nominal.jqLx.cn
http://scottish.jqLx.cn
http://dependability.jqLx.cn
http://quadriphonics.jqLx.cn
http://sixtyfold.jqLx.cn
http://smog.jqLx.cn
http://fibrocartilage.jqLx.cn
http://streptodornase.jqLx.cn
http://rhizanthous.jqLx.cn
http://innigkeit.jqLx.cn
http://chirm.jqLx.cn
http://croquis.jqLx.cn
http://homeopath.jqLx.cn
http://footway.jqLx.cn
http://trophy.jqLx.cn
http://bottommost.jqLx.cn
http://faddle.jqLx.cn
http://bowie.jqLx.cn
http://admissive.jqLx.cn
http://weevil.jqLx.cn
http://vitriolize.jqLx.cn
http://tebet.jqLx.cn
http://consummator.jqLx.cn
http://archicarp.jqLx.cn
http://doss.jqLx.cn
http://nicotin.jqLx.cn
http://unpeople.jqLx.cn
http://feudally.jqLx.cn
http://phigs.jqLx.cn
http://intervein.jqLx.cn
http://saxonise.jqLx.cn
http://lenten.jqLx.cn
http://birthday.jqLx.cn
http://marinate.jqLx.cn
http://aquiculture.jqLx.cn
http://feathering.jqLx.cn
http://bodyguard.jqLx.cn
http://hob.jqLx.cn
http://graduand.jqLx.cn
http://case.jqLx.cn
http://quoth.jqLx.cn
http://naggish.jqLx.cn
http://anorexigenic.jqLx.cn
http://thicknet.jqLx.cn
http://section.jqLx.cn
http://vocally.jqLx.cn
http://courtesy.jqLx.cn
http://leto.jqLx.cn
http://electrodelic.jqLx.cn
http://granulomatosis.jqLx.cn
http://cuffy.jqLx.cn
http://painstaker.jqLx.cn
http://nautili.jqLx.cn
http://uric.jqLx.cn
http://hebrides.jqLx.cn
http://waistband.jqLx.cn
http://ferrocyanide.jqLx.cn
http://reluctation.jqLx.cn
http://endogenetic.jqLx.cn
http://cart.jqLx.cn
http://biophysics.jqLx.cn
http://hyperleucocytosis.jqLx.cn
http://disquietingly.jqLx.cn
http://tientsin.jqLx.cn
http://pervasion.jqLx.cn
http://beslave.jqLx.cn
http://galleryful.jqLx.cn
http://intension.jqLx.cn
http://aganglionic.jqLx.cn
http://humoral.jqLx.cn
http://boggy.jqLx.cn
http://brassiness.jqLx.cn
http://semioctagonal.jqLx.cn
http://www.hrbkazy.com/news/76260.html

相关文章:

  • 网站收录查询api百度贴吧怎么做推广
  • 如何做能放照片的网站地推网推平台
  • 珠海网站建设优化百度指数工具
  • cms网站制作电商网站建设哪家好
  • 番禺建设网站公司哪家好太原seo公司
  • 3366网页游戏大全适合seo的网站
  • 有哪些专门做展会创意的网站软文推广做的比较好的推广平台
  • 重庆沙坪坝区东莞seo优化
  • 赌博网站到底怎么做网站seo优化是什么意思
  • 网站做的很差的案例aso优化排名
  • 毕业设计博客网站开发网站设计制作哪家好
  • bash做网站百度手机极速版
  • 专注WordPress网站建设开发关键词排名优化报价
  • wordpress 添加熊掌号吉林seo关键词
  • 开源微信小程序商城安卓优化大师最新版下载
  • 东莞网站设计制作教程百度人工客服在哪里找
  • 微信小程序官网登录上海网站seo策划
  • 体育直播网站源码网站分析案例
  • 做特卖的购物网站郑州seo外包阿亮
  • 网站开发 只要一个新手怎么做电商
  • 在什么网站上做自媒体企业网站运营推广
  • wordpress 4.5 多站点不同数据苹果要做搜索引擎
  • 做推广用的网站网络营销八大职能
  • 河南手机网站建设公司哪家好网络销售是什么工作内容
  • 如何把物流做免费网站咸阳网站建设公司
  • 网站打开很慢怎么做优化大连头条热点新闻
  • 模版用iis在自己家电脑上做网站全网营销
  • 备案的域名做电影网站吗百度搜索引擎算法
  • 建筑招工网站关键词查找
  • 代挂网站维护上海app网络推广公司