xml方式的序列化试试 这个类可以直接用System.Xml.Serialization.XmlSerializer来实现序列化与反序列化。C# code XmlSerializer s = new XmlSerializer(typeof(User)); string filename="文件路径"; TextWriter writer = new StreamWriter(filename); s.Serialize(writer); 参照:http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx
小薛 问你个问题啊?//序列化方法 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(@"D:\\cyj.XML",FileMode.Create,FileAccess.Write,FileShare.Read); xs.Serialize(stream,c); stream.Close(); }其中XmlSerializer xs = new XmlSerializer(typeof(Person));这句话老是报错啊? 反射的person出问题。 我看不出来啊。
这个是Person的模型 using System; using System.Data; using System.Xml.Serialization;namespace MySerializeExample { /// <summary> /// MyData 的摘要说明。 /// </summary> [Serializable] public class Person { public Person() { } private string name; public string Name { get { return name; } set { name=value; } } public string Sex; public int Age=31; public Course[] Courses; public Person(string Name) { name = Name; Sex="男"; } } [Serializable] public class Course { public string Name; [XmlIgnore] public string Description; public Course(string name,string description) { Name = name; Description = description; } } }
using System;
using System.Collections.Generic;
using System.Text;namespace SocketModule
...{
public class TagReadInfo
...{
public TagReadInfo() ...{ } private string clientIP;
private string gasStationID;
private byte[] tagReadBytes;
private byte clientFlag; /**//// <summary>
/// 客户端的IP 即PDA的IP
/// </summary>
public string ClientIP
...{
get ...{ return clientIP; }
set ...{ clientIP = value; }
} /**//// <summary>
/// 加气站编号 37010201
/// </summary>
public string GasStationID
...{
get ...{ return gasStationID; }
set ...{ gasStationID = value; }
} /**//// <summary>
/// 客户端发送的字节数组 里面包括了时间 电子标签号 识读信息 车牌号码 加气枪号
/// </summary>
public byte[] TagReadBytes
...{
get ...{ return tagReadBytes; }
set ...{ tagReadBytes = value; }
} /**//// <summary>
/// 是否是缓存数据 是:1 不是:0
/// </summary>
public byte ClientFlag
...{
get ...{ return clientFlag; }
set ...{ clientFlag = value; }
}
public byte[] ToByte()
...{
string str = clientIP + "," + gasStationID;
byte[] mess = System.Text.Encoding.UTF8.GetBytes(str.ToCharArray()); byte[] send = new byte[mess.Length + 33];
TagReadBytes.CopyTo(send, 0);
send[32] = ClientFlag;
mess.CopyTo(send, 33);
byte length = (byte)send.Length; byte[] message = new byte[send.Length + 1];
message[0] = length;
send.CopyTo(message, 1);
return message; }
public static TagReadInfo GetModel(byte[] Sendmess)
...{
byte length = Sendmess[0];
byte[] mess = new byte[length];
Buffer.BlockCopy(Sendmess, 1, mess, 0, length); TagReadInfo info = new TagReadInfo();
byte[] mb = new byte[32];
Buffer.BlockCopy(mess, 0, mb, 0, 32);
info.TagReadBytes = mb;
info.ClientFlag = mess[32]; byte[] message = new byte[mess.Length - 33];
Buffer.BlockCopy(mess, 33, message, 0, mess.Length - 33);
string[] infos = System.Text.Encoding.UTF8.GetString(message).Split(',');
info.ClientIP = infos[0];
info.GasStationID = infos[1];
return info;
} public static TagReadInfo GetModelForPDA(byte[] Sendmess)
...{
byte length = Sendmess[0];
byte[] mess = new byte[length];
Buffer.BlockCopy(Sendmess, 1, mess, 0, length); TagReadInfo info = new TagReadInfo();
byte[] mb = new byte[32];
Buffer.BlockCopy(mess, 0, mb, 0, 32);
info.TagReadBytes = mb;
info.ClientFlag = mess[32]; byte[] message = new byte[mess.Length - 33];
Buffer.BlockCopy(mess, 33, message, 0, mess.Length - 33);
string[] infos = System.Text.Encoding.UTF8.GetString(message,0,message.Length).Split(',');
info.ClientIP = infos[0];
info.GasStationID = infos[1];
return info;
}
}
}
这个类可以直接用System.Xml.Serialization.XmlSerializer来实现序列化与反序列化。C# code XmlSerializer s = new XmlSerializer(typeof(User)); string filename="文件路径"; TextWriter writer = new StreamWriter(filename); s.Serialize(writer);
参照:http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx
问你个问题啊?//序列化方法
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(@"D:\\cyj.XML",FileMode.Create,FileAccess.Write,FileShare.Read);
xs.Serialize(stream,c);
stream.Close();
}其中XmlSerializer xs = new XmlSerializer(typeof(Person));这句话老是报错啊? 反射的person出问题。 我看不出来啊。
using System;
using System.Data;
using System.Xml.Serialization;namespace MySerializeExample
{
/// <summary>
/// MyData 的摘要说明。
/// </summary>
[Serializable]
public class Person
{
public Person()
{ }
private string name;
public string Name
{
get
{
return name;
}
set
{
name=value;
}
}
public string Sex;
public int Age=31;
public Course[] Courses;
public Person(string Name)
{
name = Name;
Sex="男";
}
}
[Serializable]
public class Course
{
public string Name;
[XmlIgnore]
public string Description;
public Course(string name,string description)
{
Name = name;
Description = description;
}
}
}