ASP.NET 对类进行XML序列化和反序列化
1 序列化方法
public void SerializeObject<T>(string Xmlname,T t)
{
XmlSerializer ser = new XmlSerializer(typeof(T)); TextWriter writer = new StreamWriter(Xmlname);
ser.Serialize(writer, t);//要序列化的对象
writer.Close();
}
2 序列化方法的使用
NodeConfigInfo nc = new NodeConfigInfo();
nc.FirstNodeSql = this.textBoxSqlFirstNode.Text.Trim();
nc.FirstArticleSql = this.textBoxViewFirArtlcle.Text.Trim();
nc.SecondNodeSql = this.textBoxSqlSecondNode.Text.Trim();
nc.SecondArticleSql = this.textBoxViewSecArtlcle.Text.Trim();
nc.SecondNodeFilter = this.textBoxSecFilter.Text.Trim();
nc.SecondArticleFilter = this.textBoxSecFilter.Text.Trim();
nc.SecondNodeDefaultParentId = this.textBoxParentDefault.Text.Trim();
nc.ConnectionOther = this.textBoxConnection.Text.Trim();
nc.NodeId = this.textBoxParentId.Text.Trim();
nc.SecondReFilter = this.textBoxReFilterSec.Text.Trim(); string xmlName = System.IO.Directory.GetCurrentDirectory() + "\\" + typeof(NodeConfigInfo).Name + ".Config";
SerializeObject<NodeConfigInfo>(xmlName, nc);
3 序列化的类
public class NodeConfigInfo
{
public string FirstNodeSql { get; set; } public string FirstArticleSql { get; set; } public string SecondNodeSql { get; set; } public string SecondArticleSql { get; set; } public string SecondNodeFilter { get; set; } public string SecondArticleFilter { get; set; } public string SecondNodeDefaultParentId { get; set; } public string ConnectionOther { get; set; } public string NodeId { get; set; } public string SecondReFilter { get; set; }
}
4 反序列化方法
public T DeSerializeObject<T>()
{
string xmlName = System.IO.Directory.GetCurrentDirectory() + "\\" + typeof(T).Name + ".Config";
FileStream file = new FileStream(xmlName, FileMode.Open, FileAccess.Read);
XmlSerializer xmlSearializer = new XmlSerializer(typeof(T));
T info = (T)xmlSearializer.Deserialize(file);
file.Close();
file.Dispose();
return info;
}
5 反序列化方法的使用
//xml来源可能是外部文件,也可能是从其他系统获得
NodeConfigInfo info = DeSerializeObject<NodeConfigInfo>(); this.textBoxSqlFirstNode.Text = info.FirstNodeSql;
this.textBoxViewFirArtlcle.Text = info.FirstArticleSql;
this.textBoxSqlSecondNode.Text = info.SecondNodeSql;
this.textBoxViewSecArtlcle.Text = info.SecondArticleSql;
this.textBoxSecFilter.Text = info.SecondNodeFilter;
this.textBoxSecFilter.Text = info.SecondArticleFilter;
this.textBoxParentDefault.Text = info.SecondNodeDefaultParentId;
this.textBoxConnection.Text = info.ConnectionOther;
this.textBoxParentId.Text = info.NodeId;
this.textBoxReFilterSec.Text = info.SecondReFilter;
ASP.NET 对类进行XML序列化和反序列化的更多相关文章
- c# XML和实体类之间相互转换(序列化和反序列化)[砖]
link: http://blog.okbase.net/haobao/archive/62.html by: 好饱 我们需要在XML与实体类,DataTable,List之间进行转换,下面是XmlU ...
- C# XML和实体类之间相互转换(序列化和反序列化)
我们需要在XML与实体类,DataTable,List之间进行转换,下面是XmlUtil类,该类来自网络并稍加修改. using System; using System.Collections.Ge ...
- Xml序列化、反序列化帮助类
之前从网络上找了一个Xml处理帮助类,并整理了一下,这个帮助类针对Object类型进行序列化和反序列化,而不需要提前定义Xml的结构,把它放在这儿供以后使用 /// <summary> / ...
- XML和实体类之间相互转换(序列化和反序列化)
我们需要在XML与实体类,DataTable,List之间进行转换,下面是XmlUtil类,该类来自网络并稍加修改. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
- .NET中XML序列化和反序列化常用类和用来控制XML序列化的属性总结(XmlSerializer,XmlTypeAttribute,XmlElementAttribute,XmlAttributeAttribute,XmlArrayAttribute...)
序列化和反序列化是指什么? 序列化(seriallization): 将对象转化为便于传输的数据格式, 常见的序列化格式:二进制格式,字节数组,json字符串,xml字符串.反序列化(deserial ...
- 对类参数的序列化和反序列化XML
/// <summary> /// Xml序列化与反序列化 /// </summary> public class XmlUtil { #region 反序列化 /// < ...
- Windows phone 之XML序列化与反序列化
为什么要做序列化和反序列化? 一个回答: 我们都知道对象是不能在网络中直接传输的,不过还有补救的办法.XML(Extensible Markup Language)可扩展标记语言,本身就被设计用来存储 ...
- XML 序列化与反序列化
XML序列化与反序列化 1.将一个类转化为XML文件 /// <summary> /// 对象序列化成XML文件 /// </summary> /// <param na ...
- XmlSerializer 对象的Xml序列化和反序列化
http://www.cnblogs.com/yukaizhao/archive/2011/07/22/xml-serialization.html 这篇随笔对应的.Net命名空间是System.Xm ...
随机推荐
- STM32启动文件深度解析
STM32启动过程全面解析,包括启动过程的介绍.启动代码的陈列以及深入解析.相对于ARM上一代的主流ARM7/ARM9内核架构,新一代Cortex内核架构的启动方式有了比较大的变化.ARM7/ARM9 ...
- CSS-div高度100%设置问题
div常用的属性width和height,有的时候如果我们需要让div的高度是整个屏幕的高度,设置height:100%发现并没有什么作用,并不是这样设置不对,而是w3c规范中关于百分比的设置是相对于 ...
- 一款基于jQuery的图片场景标注提示弹窗特效
今天给大家分享一款基于jQuery的图片场景标注提示弹窗特效,这款实例适合在图片上标注某个物件,单击弹出详情说明,兼容360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之 ...
- JQuery实现可编辑的表格
点击表格后可直接编辑,回车或鼠标点击页面其他地方后编辑生效,按Esc可取消编辑 第一种单击表格可以编辑的方法 //相当于在页面中的 body标签加上onload事件$(function() { ...
- C#获取程序启动目录
//WCF service: string servicePath = System.Web.Hosting.HostingEnvironment.MapPath("~"); // ...
- 转: gcc 指定运行时动态库路径
gcc 指定运行时动态库路径 Leave a reply 由于种种原因,Linux 下写 c 代码时要用到一些外部库(不属于标准C的库),可是由于没有权限,无法将这写库安装到系统目录,只好安装用户目录 ...
- [Algorithm] Construct String from Binary Tree
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
- Python requests如何将第一个请求得到的 cookie 通过 POST 提交给第二个请求
#coding=utf-8 import requests import json url_login, url_test = "http://192.168.0.162/login&quo ...
- Lintcode: Unique Paths
C++ dp 递推式:dp[i][j] = dp[i-1][j] + dp[i][j-1] 初值:dp[i][j] = 1,i=0 or j=0 空间优化:省掉一维 class Solution { ...
- Struts2添加了<s:debug>后页面无效果的解决方案
一.环境 Struts2版本 struts2.5 二.问题 在jsp页面中添加了<s:debug>后页面上无任何展示. 三.解决 在struts.xml中的struts节点下添加如下常量即 ...