您的位置:首页 > 编程语言 > C#

学习C# XmlSerializer 序列化反序列化XML

2012-04-01 15:26 155 查看
类、变量常用头:

[XmlRootAttribute]:对根节点的描述,在类声明中使用 如:下例的Html类

[XmlType]:对节点描述,在类声明中使用      如:下例的Head类

[XmlElement]:节点下内部节点描述,如果对数组标识,是对数组单元描述 如:下例的Html.body,Head.title,Head.metas和Head.scripts数组...

[XmlAttribute]:节点下内部属性描述 如:下例的Meat.httpequiv,Meat.content,Script.src,Script.type,...

[XmlArrayItem]:数组单元项描述 如:下例的Body.lis

[XmlArray]:数组描述 如:下例的Body.lis

[XmlIgnore]:使该项不序列化             如:下例的Meta.data

[XmlText]:做为节点的text文本输出          如:下例的Script.content,Li.Content...

例如:

类定义代码

using System;
using System.Xml.Serialization;

[XmlRootAttribute("html")]
public class Html
{
public Head head { get; set; }

[XmlElement("body")]
public Body body { get; set; }
}

[XmlType("head")]
public class Head
{
[XmlElement("title")]
public string titile;

[XmlElement("meta")]
public Meta[] metas;

[XmlElement("script")]
public Script[] scripts;
}

/// <summary>
/// http-equiv="Content-Type" content="text/html; charset=utf-8"
/// </summary>
public class Meta
{
[XmlAttribute("http-equiv")]
public string httpEquiv;

[XmlAttribute]
public string content;

[XmlIgnore]
public string  data;
}

/// <summary>
///  script src="/script/common.js" type="text/javascript"
/// </summary>
public class Script
{
[XmlAttribute]
public string src;
[XmlAttribute]
public string type;

[XmlText]
public string content;
}

public class Body
{
[XmlElement("table")]
public List<Table> tables=new List<Table>();

[XmlArray("ui")]
[XmlArrayItem("li")]
public List<Li> Lis = new List<Li>();
}

public class Li
{
[XmlText]
public string content;
}

public class Table
{
[XmlAttribute]
public string height;
[XmlAttribute]
public string width;

[XmlText]
public string content;
}


序列化

System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Html));
Html html = new Html();
Head head = new Head();
head.title = "这是一个示例";
Meta[] metaArray = new Meta[1];
metaArray[0] = new Meta() { httpEquiv = "Content-Type", content = "text/html; charset=utf-8", data="该数据不被序列化" };
Script[] scriptArray = new Script[2];
scriptArray[0] = new Script() { type = "text/javascript", src = "/script/jquery.js" };
scriptArray[1] = new Script() { type = "text/javascript", content = "var number=6; alert('这是一个示例number='+number);" };
head.metas = metaArray;
head.scripts = scriptArray;
Body body = new Body();
body.tables.Add(new Table() { height = "5", width = "4", content = "这是table1" });
body.tables.Add(new Table() { content = "这是table2" });
body.Lis.Add(new Li() { content = "li1" });
body.Lis.Add(new Li() { content = "li2" });
html.head = head;
html.body = body;
string serializerString = "";
using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
System.IO.TextWriter writer = new System.IO.StreamWriter(stream, Encoding.UTF8);
System.Xml.Serialization.XmlSerializerNamespaces ns = new System.Xml.Serialization.XmlSerializerNamespaces();
ns.Add("", "");//不输出xmlns
serializer.Serialize(writer, html, ns);
stream.Position = 0;
byte[] buf = new byte[stream.Length];
stream.Read(buf, 0, buf.Length);
serializerString= System.Text.Encoding.UTF8.GetString(buf);
}


serializerString值为:

<?xml version="1.0" encoding="utf-8"?>
<html>
<head>
<title>这是一个示例</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="/script/jquery.js" type="text/javascript" />
<script type="text/javascript">var number=6; alert('这是一个示例number='+number);</script>
</head>
<body>
<table height="5" width="4">这是table1</table>
<table>这是table2</table>
<ui>
<li>li1</li>
<li>li2</li>
</ui>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: