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

新变更营业执照注册号查了发现之前有备案过网站了网站建设主要业务流程设定

新变更营业执照注册号查了发现之前有备案过网站了,网站建设主要业务流程设定,wordpress置顶重复了,点餐小程序开发需要多少钱在我们深入探时C#序列化和反序列化,之前我们先要明白什么是序列化,它又称串行化,是.ET运行时环境用来支持用户定义 类型的流化的机制。序列化就是把一个对象保存到一个文件或数据库字段中去,反序列化就是在适当的时候把这个文件再…

在这里插入图片描述
在我们深入探时C#序列化和反序列化,之前我们先要明白什么是序列化,它又称串行化,是.ET运行时环境用来支持用户定义
类型的流化的机制。序列化就是把一个对象保存到一个文件或数据库字段中去,反序列化就是在适当的时候把这个文件再转化
成原来的对象使用。其目的是以某种存储形成使自定义对象持久化,或者将这种对像从一个地方传输到另一个地方。.ET框
架提供了两种串行化的方式:
1、是使用Binary Formatteri进行串行化;
2、使用Soap Formatteri进行串行化;
3、使用XmlSerializeri进行串行化。
第一种方式提供了一个简单的二进制数据流以及某些附加的类型信息,而第二种将数据流格式化为XL存储;第三种其实和
第二种差不多也是XL的格式存储,只不过比第二种的XML格式要简化很多(去掉了SOAP特有的额外信息)。可以使用
[Serializable]属性将类标志为可序列化的。如果某个类的元素不想被序列化,1、2可以使用NonSerialized属性来标志,2、
可以使用[Xmllg nore]来标志。
下面就让上我们开始深入了解C序列化和反序列化:
C#序列化和反序列化1、使用BinaryFormatteri进行串行化
下面是一个可串行化的类:
代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.Ul;
using System.Web.Ul.WebControls;
using System.Web.Ul.WebControls.WebParts;
using System.Web.Ul.HtmlControls;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
/*summary
/ClassToSerialize的摘要说明
//</summary>
[Serializable]
public class ClassToSerialize
public int id =100;
public string name ="Name";
[NonSerialized
public string Sex=“男";
下面是串行化和反串行化的方法:
代码如下:
public void SerializeNow()
ClassToSerialize c=new ClassToSerialize();
FileStream fileStream
new FileStream("c:\\temp.dat",FileMode.Create);
BinaryFormatter b new BinaryFormatter();
b.Serialize(fileStream,c);
fileStream.Close();
public void DeSerializeNow()
ClassToSerialize c=new ClassToSerialize();
c.SeX=“kkkk";
FileStream fileStream
new FileStream("c:\temp.dat",
FileMode.Open,FileAccess.Read,FileShare.Read);
BinaryFormatter b new BinaryFormatter();
c b.Deserialize(fileStream)as ClassToSerialize;
Response.Write(c.name);
Response.Write(c.Sex);
fileStream.Close();

调用上述两个方法可以看到串行化的结果:Sex属性因为被标志为NonSerialized,故其值总是为null,

C#序列化和反序列化2、使用SoapFormatteri进行串行化和BinaryFomatter类似,我们只需要做一下简单修改即可:

a.将usingi语句中的.Formatter.Binaryi改为.Formatter.Soap;

b.将所有的BinaryFormatteri替换为Soap Formatter.

c.确保报存文件的扩展名为.xml

经过上面简单改动,即可实现SoapFormatter的串行化,这时候产生的文件就是一个ml格式的文件。

C#序列化和反序列化3、使用KmlSerializeri进行串行化

关于格式化器还有一个问题,假设我们需要XL,但是不想要SO八P特有的额的外信息,那么我们应该怎么办呢?有两中方案:

要么编写一个实现IFormatter接口的类,采用的方式类似于SoapFommatter类,但是没有你不需要的信息;要么使用库类

XmlSerializer,这个类不使用Serializable属性,但是它提供了类似的功能。

如果我们不想使用主流的串行化机制,而想使用XmlSeralizer进行串行化我们需要做一下修改:

a.添加System.Xml.Serialization命名空间。

b.Serializable和NoSerialized,属性将被忽略,而是使用Kmllgnore属性,它的行为与NoSerialized类似。

c.XmlSeralizer要求类有个默认的构造器,这个条件可能已经满足了。

下面看C#序列化和反序列化示例:

要序列化的类:

代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.Ul;
using System.Web.Ul.WebControls;
using System.Web.Ul.WebControls.WebParts;
using System.Web.Ul.HtmlControls;
using System.Xml.Serialization;
代码如下:
[Serializable]
public class Person
private string name;
public string Name
get
{
<SPAN style="WHITE-SPACE:pre"></SPAN>return name;
<SPAN style="WHITE-SPACE:pre"></SPAN>)
<SPAN style="WHITE-SPACE:pre"></SPAN>set
<SPAN style="WHITE-SPACE:pre"></SPAN>{
<SPAN style="WHITE-SPACE:pre"></SPAN>
name value;
<SPAN style="WHITE-SPACE:pre"></SPAN>)
public string Sex;
public int Age =31;
public Course[]Courses;
public Person()
public Person(string Name)
<SPAN style="WHITE-SPACE:pre"></SPAN>name Name;
<SPAN style="VHITE-SPACE:pre"</SPAN>Sex=“男”;
}

代码如下:

[Serializable]
public class Course
{
public string Name;
[Xmllgnore]
public string Description;
public Course()
public Course(string name,string description)
<SPAN style="WHITE-SPACE:pre"></SPAN>Name name;
<SPAN style="WHITE-SPACE:pre"></SPAN>Description description;
}

C#序列化和反序列化方法:
代码如下:

public void XMLSerialize()
Person c new Person("cyj");
c.Courses new Course[2];
c.Courses[0]=new Course(英语”,“交流江具):
c.Courses[1]=new Course(数学”,自然科学"):
XmlSerializer xs new XmlSerializer(typeof(Person));
Stream stream new FileStream("c:\cyj.XML",FileMode.Create,FileAccess.Write,FileShare.Read);
xs.Serialize(stream,c);
stream.Close();
public void XMLDeserialize()
XmlSerializer xs new XmlSerializer(typeof(Person));
Stream stream new FileStream("C:\\cyj.XML",FileMode.Open,FileAccess.Read,FileShare.Read);
Person p xs.Deserialize(stream)as Person;
Response.Write(p.Name);
Response.Write(p.Age.ToString());
Response.Write(p.Courses[0].Name);
Response.Write(p.Courses[0].Description);
Response.Write(p.Courses[1].Name);
Response.Write(p.Courses[1].Description);
stream.Close();

这里Course类的Description属性值将始终为null,生成的xml文档中也没有该节点,如下:
代码如下:

xml version="1.0">
Person xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
Sex>/Sex>
Age>31 </Age>
Courses
×Course>
Name>英语Name>
Description>交流工具'Description>
×/Course>
Course>
Name>数学Name>
Description>自然科学Description>
</Course>
</Courses>
×Name>cyj </Name>
</Person>

C#序列化和反序列化4、自定义序列化

如果你希望让用户对类进行串行化,但是对数据流的组织方式不完全满意,那么可以通过在自定义类中实现接口来自定义串行化行为。这个接口只有一个方法,GetObjectData.这个方法用于将对类对象进行串行化所需要的数据填进SerializationInfo)对象。你使用的格式化器将构造SerializationInfo对象,然后在串行化时调用GetObjectData.如果类的父类也实现了ISerializable,那么应该调用GetObjectDatal的父类实现。如果你实现了ISerializable,那么还必须提供一个具有特定原型的构造器,这个构造器的参数列表必须与GetObjectData相同。这个构造器应该被声明为私有的或受保护的,以防止粗心的开发人员直接使用它。示例如下:

C#序列化和反序列化之实现ISerializable的类:

代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.Ul;
using System.Web.Ul.WebControls;
using System.Web.Ul.WebControls.WebParts;
using System.Web.Ul.HtmlControls;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
*lWW×summary>
/Employee的摘要说明
1//</summary
[Serializable]
public class Employee:ISerializable
public int Empld=100;
public string EmpName-"刘德华';
[NonSerialized]
public string NoSerialString ="NoSerialString-Test";
public Employee()
<SPAN style=-"WHITE-SPACE:pre"</SPAN>∥TODO:在比处添加构造函数逻辑
<SPAN style--"WHITE-SPACE:pre”></SPAN>}
private Employee(Serializationlnfo info,StreamingContext ctxt)
<SPAN style="WHITE-SPACE:pre"></SPAN>Empld =(int)info.GetValue("Employeeld",typeof(int));
<SPAN style="WHITE-SPACE:pre"></SPAN>EmpName =(String)info.GetValue("EmployeeName",typeof(string));
<SPAN style="WHITE-SPACE:pre"></SPAN>//NoSerialString =(String)info.GetValue("EmployeeString",typeof(string));
public void GetObjectData(Serializationlnfo info,Streaming Context ctxt)
<SPAN style="WHITE-SPACE:pre"></SPAN>info.AddValue("Employeeld",Empld);
<SPAN style="WHITE-SPACE:pre"></SPAN>info.AddValue("EmployeeName",EmpName);
<SPAN style="WHITE-SPACE:pre"></SPAN>//info.AddValue("EmployeeString",NoSerialString);
}

C#序列化和反序列化方法:
代码如下:

public void OtherEmployeeClassTest()
Employee mp new Employee();
mp.Empld 10;
mp.EmpName=“邱t枫”;
mp.NoSerialString=“你好呀";
Stream steam File.Open("c:\temp3.dat",File Mode.Create);
BinaryFormatter bf new BinaryFormatter();
Response.Write("Writing Employee Info:")
bf.Serialize(steam,mp);
steam.Close();
mp null;
C#序列化和反序列化之反序列化
Stream steam2 File.Open("c:\temp3.dat",FileMode.Open);
BinaryFormatter bf2 new BinaryFormatter();
Response.Write("Reading Employee Info:")
Employee mp2 =(Employee)bf2.Deserialize(steam2);
steam2.Close();
Response.Write(mp2.Empld);
Response.Write(mp2.EmpName);
Response.Write(mp2.NoSerialString);

这里介绍了几种方式之间的序列化与反序列化之间的转换
首先介绍的如何序列化,将object对象序列化常见的两种方式即string和xml对象;
第一种将object转换为string对象,这种比较简单没有什么可谈的;

http://www.yayakq.cn/news/282159/

相关文章:

  • 找客户软件百度seo排名规则
  • 长沙的在线商城网站建设wordpress 亲子主题
  • 网站制作方案专业乐云seo正规网站建设公司一般要多少钱
  • 网站建设方面存在的问题东莞厂房招标平台
  • 湖北省和住房建设厅官方网站自己怎么做简单的网站
  • aws网站建设wordpress update_option
  • 大恒建设集团有限公司网站wordpress 全局变量
  • 网站怎么做移动图片大全国内高清图片素材网站推荐
  • 海口哪里做网站公司制作网站系统
  • 松原市网站建设域名怎么卖
  • 网站静态页广州网络公司策划
  • 沈阳营销型网站制作技术网站常用图片
  • 微网站建设流程创建公众号的流程
  • 韩国做 mp4下载网站营销的三个基本概念是什么
  • 企业网站app开发平台济南网页制作公司
  • 夸克破解可看禁用网站杭州知名的网站建设策划
  • 网站建设评比自评情况管网建设是什么意思
  • 苏州手机网站设计网站开发 书
  • 黄冈网站建设效果电商网站开发成本
  • 做网站导航cms网站国际化建设方案
  • 百度指数资讯指数是指什么网站seo快速优化技巧
  • 网站建设的杂志工程咨询公司
  • 源码制作网站网站导航规划
  • 哪个网站可以自己做行程做一样的模板网站会被告侵权吗
  • 给企业建设网站的意义自己建网站怎么做seo
  • 增城做网站公司用c 做网站
  • 职业教育网站开发爱做网站免费模板vip
  • 网站欢迎页面怎么做剪辑素材网站
  • 宣城网站开发专业制温州网站建设 首选国鼎网络好
  • 宁波网站制作公司排名wordpress4.9.4