.net的XML对象序列化VS WCF中xml序列化问题

时间:2022-04-07 23:33:33

整理一下 .net 对象序列化注意事项:

1. 字段:必须是 public类型

2.属性:只读或者只写的属性不被序列化,只有 可读可写并且赋值的才可以

序列化:

Someclass obj = new SomeClass();

XmlSerializer ser = new XmlSerializer(typeof(Someclass));

StreamWriter writer = new StreamWriter("ser.xml");

ser.Serialize(writer, obj);

writer.Close();

反序列化

FileStream fs = new FileStream("ser.xml", FileMode.Open);

Someclass newObj = (Someclass)ser.Deserialize(fs);

fs.Close();

对于数组亦可,只需将SomeClass换成SomeClass[]即可。

关于在WCF中的序列化

json:DataContractJsonSerializer支持使用以JavaScript 对象标记作为序列化格式

using System.Runtime.Serialization.Json;

Employee e = new Employee(, "Daniel", "Dong");
FileStream writer = new FileStream("sample.xml", FileMode.Create);
DataContractJsonSerilizer ser = new DataContractJsonSerilizer(typeof(Employee));
ser.Serialize(writer, e);
writer.Close();

序列化Employee类的结果

{"Employee":101, "FirstName":"Daniel", "LastName":"Dong"}

xml: 主要讲两种

DataContractSerializer(作为默认的序列化基础方法因为它是WCF的初始序列化结构)、

XmlSerializer(如果你需要支持已有的类型或者自定义序列化而且它们不被DataContractSerializer支持的话,你可能想要使用XmlSerializer.)

Demo: xmlSerializer

using System.Xml.Serialization;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Diagnostics; namespace Chinaer.WcfDemo.ConsoleClient
{
class Program
{
static void Main(string[] args)
{ Person person = new Person()
{
Age = ,
UserPwd = "",
UserName = "郭志奇",
Date = DateTime.Now
}; using (XmlWriter writer = new XmlTextWriter("person.xml", Encoding.UTF8))
{
XmlSerializer serializer = new XmlSerializer(typeof(Person));
serializer.Serialize(writer, person);
} Process.Start("person.xml");
Console.Read();
} /// <summary>
/// 序列化方法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="instace"></param>
/// <param name="fileName"></param>
public static void Serialize<T>(T instace, string fileName)
{
using (XmlWriter writer = new XmlTextWriter(fileName, Encoding.UTF8))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
serializer.Serialize(writer, instace);
}
Process.Start(fileName);
}
}
[XmlRoot(ElementName="guozhiqi",Namespace="http://www.guozhiqi.com")]
/// <summary>
/// 定义一个实体类 Person
/// </summary>
public class Person
{
private Guid _id; private DateTime _date;
//注意我们没有默认的构造函数
internal double Age { get; set; } //私有字段 年龄
/// <summary>
/// 通过XmlAttributeAttribute 序列化成xml属性
/// </summary>
[XmlAttribute(AttributeName="GuidID",Namespace="http://guidID")]
public Guid ID
{
get { return _id; }
set
{
_id = value;
}
} //公有的随机数
[XmlElement(ElementName="DateTime",Namespace="http://date")]
public DateTime Date
{
set
{
_date = value;
}
get
{
return _date;
}
} public string UserName { get; set; } public string UserPwd { get; set; }
public Person() { }
public Person(double age, Guid id)
{
this.Age = age; }
} }

序列化结果:

<?xml version="1.0" encoding="utf-8"?>

<guozhiqi xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 xmlns:xsd="http://www.w3.org/2001/XMLSchema"

 d1p1:GuidID="00000000-0000-0000-0000-000000000000"
xmlns:d1p1="http://guidID" xmlns="http://www.guozhiqi.com">
<DateTime xmlns="http://date">--21T23::13.8584896+:</DateTime>
<UserName>郭志奇</UserName><UserPwd></UserPwd></guozhiqi>

注意:xmlSerializer,对字段、属性序列化 遵循.net 序列化规则,字段是public,属性是可读可写,并且我们还也可以指定名称及命名空间

参考:

tks:

http://blog.sina.com.cn/s/blog_6f6769b50100z32v.html

http://www.cnblogs.com/jiagoushi/archive/2013/03/21/2974523.html

http://www.cnblogs.com/danielWise/archive/2010/12/17/1909537.html