悲剧了,遇到个问题,不知道怎么解决,请高手出马,新人分数比较少,请见谅!类似与这样的XML如何反序列化成实体类,或者什么样的实体类能序列化成如下的XML,
<ManValueList>
<Item Name="Industry">hangye1</Item>
<Item Name="Language">Chs</Item>
<Item Name="IdentifyingCode">Complete</Item>
<Item Name="ExistedQuestion">1</Item>
<Item Name="RegEmailActivation">1</Item>
<Item Name="RegManCheck">1</Item>
<Item Name="MakeDifficulty">Easy</Item>
</ManValueList>
<ManValueList>
<Item Name="Industry">hangye1</Item>
<Item Name="Language">Chs</Item>
<Item Name="IdentifyingCode">Complete</Item>
<Item Name="ExistedQuestion">1</Item>
<Item Name="RegEmailActivation">1</Item>
<Item Name="RegManCheck">1</Item>
<Item Name="MakeDifficulty">Easy</Item>
</ManValueList>
难道序列化也要自己写 T_T
{
static void Main(string[] args)
{
Serialize(); Deserialize(); Console.ReadLine();
} /*
* 使用XmlSerializer序列化类时,该类不需要必须要标记[Serializable]属性。
* 需要有不带任何参数的构造函数。
* 并且只可以序列化公有的成员,私有成员和保护成员在序列化中会丢失。
*/ public static void Serialize()
{
Console.WriteLine("---Serialize---"); Employee emp = new Employee("Daniel", 100, "Guangzhou", new string[] { "020-82319259", "13570479050", "020-12345678"});
Console.WriteLine(emp.ToString()); FileStream stream = new FileStream(@"C:Employee.xml", FileMode.Create);
XmlSerializer serializer = new XmlSerializer(typeof(Employee)); serializer.Serialize(stream, emp);
stream.Close();
} public static void Deserialize()
{
Console.WriteLine("---Deserialize---"); FileStream stream = new FileStream(@"C:Employee.xml", FileMode.Open);
XmlSerializer serializer = new XmlSerializer(typeof(Employee)); Employee emp = (Employee)serializer.Deserialize(stream);
stream.Close(); Console.WriteLine(emp.ToString());
}
} public class Employee
{
[XmlAttribute]
public string Name;
public int Age; // 公有变量,但不初始化,也会被序列化
private int Salary;
protected string Address;
[XmlArray(ElementName="Telephones")]
[XmlArrayItem(ElementName="Telephone")]
public string[] Telephone; // 为了序列化,所以必须要有不带任何参数的构造函数
public Employee()
{
} public Employee(string name, int salary, string address, string[] telephone)
{
this.Name = name;
this.Salary = salary;
this.Address = address;
this.Telephone = telephone;
} public override string ToString()
{
return string.Format("Name:{0}, Age:{1}, Salary:{2}, Address:{3}", this.Name, this.Age, this.Salary, this.Address);
}
}
{
static void Main(string[] args)
{
Serialize(); Deserialize(); Console.ReadLine(); } /*
* 使用自定义序列化类时,必须要标记[Serializable]属性,并且要实现ISerializable接口。
*/ public static void Serialize()
{
Employee emp = new Employee("Daniel", 100, "Guangzhou");
Console.WriteLine("before serialize {0} ", emp.ToString()); IFormatter formatter = new SoapFormatter();
FileStream stream = new FileStream(@"C:Employee.soap", FileMode.Create);
formatter.Serialize(stream, emp);
stream.Close();
} public static void Deserialize()
{
IFormatter formatter = new SoapFormatter();
Stream stream = new FileStream(@"C:Employee.soap", FileMode.Open);
Employee emp = (Employee)formatter.Deserialize(stream);
stream.Close(); Console.WriteLine("after serialize {0} ", emp.ToString());
}
} [Serializable]
public class Employee : ISerializable
{
public string Name;
public int Age; // 公有变量,但不初始化,也会被序列化
private int Salary;
protected string Address; public Employee(string name, int salary, string address)
{
this.Name = name;
this.Salary = salary;
this.Address = address;
} protected Employee(SerializationInfo info, StreamingContext context)
{
this.Name = "FromFile:" + info.GetString("Name")
this.Salary = info.GetInt32("Salary");
this.Address = "FromFile:" + info.GetString("Address");
} public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Name", "ToFile:" + this.Name);
info.AddValue("Salary", this.Salary);
info.AddValue("Address", "ToFile:" + this.Address);
} public override string ToString()
{
return string.Format("Name={0}, Age={1}, Salary={2}, Address={3}", this.Name, this.Age, this.Salary, this.Address);
}
}
/// 序列化
/// </summary>
public static void Serialize()
{
Man man = new Man();
man.Industry = "abc";
man.IdentifyingCode = "abc";
man.Language = "英语";
man.MakeDifficulty = "嗯";
man.RegEmailActivation = "哈哈";
man.RegManCheck = "yes";
man.ExistedQuestion = "yes"; XmlSerializer xml = new XmlSerializer(typeof(Man), "");
Stream stream = new FileStream("c.xml", FileMode.Create, FileAccess.Write, FileShare.Read);
xml.Serialize(stream, man);
stream.Close();
} /// <summary>
/// 反序列化
/// </summary>
/// <returns></returns>
public static string Deserialize()
{
XmlSerializer xs = new XmlSerializer(typeof(Man));
Stream stream = new FileStream("c.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
Man man = xs.Deserialize(stream) as Man;
stream.Close(); StringBuilder sb = new StringBuilder();
sb.Append(man.ExistedQuestion);
sb.Append(man.IdentifyingCode);
sb.Append(man.Industry);
sb.Append(man.Language);
sb.Append(man.MakeDifficulty);
sb.Append(man.RegEmailActivation);
sb.Append(man.RegManCheck); return sb.ToString().Trim();
}
比如 DataTable.ToXML / FromXML
[XmlRoot("ManValueList")]
public class MyXml
{
[XmlElement("Item")]
public List<OneItem> Items { get; set; } public MyXml()
{
this.Items = new List<OneItem>();
} }
[Serializable]
public class OneItem
{
[XmlAttribute("Name")]
public string Name { get; set; }
[XmlText]
public string Text { get; set; }
} MyXml ManValueList = new MyXml();
XmlSerializer xs = new XmlSerializer(typeof(MyXml));
XmlTextReader xtr = new XmlTextReader("XMLFile1.xml");//我把你的xml文件放到这个文件里了
ManValueList = xs.Deserialize(xtr) as MyXml;
xtr.Close();