博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
学习C# XmlSerializer 序列化反序列化XML
阅读量:5274 次
发布时间:2019-06-14

本文共 4365 字,大约阅读时间需要 14 分钟。

类、变量常用头:

[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...

例如:

类定义代码

1 using System;  2 using System.Xml.Serialization;  3  4  [XmlRootAttribute("html")]  5     public class Html  6     {
7 public Head head { get; set; } 8 9 [XmlElement("body")] 10 public Body body { get; set; } 11 } 12 13 [XmlType("head")] 14 public class Head 15 {
16 [XmlElement("title")] 17 public string titile; 18 19 [XmlElement("meta")] 20 public Meta[] metas; 21 22 [XmlElement("script")] 23 public Script[] scripts; 24 } 25 26 /// 27 /// http-equiv="Content-Type" content="text/html; charset=utf-8" 28 /// 29 public class Meta 30 {
31 [XmlAttribute("http-equiv")] 32 public string httpEquiv; 33 34 [XmlAttribute] 35 public string content; 36 37 [XmlIgnore] 38 public string data; 39 } 40 41 /// 42 /// script src="/script/common.js" type="text/javascript" 43 /// 44 public class Script 45 {
46 [XmlAttribute] 47 public string src; 48 [XmlAttribute] 49 public string type; 50 51 [XmlText] 52 public string content; 53 } 54 55 public class Body 56 { 57 [XmlElement("table")] 58 public List
tables=new List
  • Lis = new List
  • (); 59 60 [XmlArray("ui")] 61 [XmlArrayItem("li")] 62 public List
  • (); 63 } 64 65 public class Li 66 {
    67 [XmlText] 68 public string content; 69 } 70 71 public class Table 72 {
    73 [XmlAttribute] 74 public string height; 75 [XmlAttribute] 76 public string width; 77 78 [XmlText] 79 public string content; 80 }
  • 序列化

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

    serializerString值为:

    这是一个示例
    这是table1
    这是table2
  • li1
  • li2
  • 转载于:https://www.cnblogs.com/xpvincent/p/3180674.html

    你可能感兴趣的文章
    JAVA 入门(一)
    查看>>
    在.net2.0中实现Action和Func方法
    查看>>
    解释一下python中的//,%和**运算符
    查看>>
    VC++6.0中不兼容问题
    查看>>
    websphere、weblogic上JConsole的配置
    查看>>
    PHP中的数组
    查看>>
    ssd a
    查看>>
    在ASP.NET AJAX中使用应用程序服务和本地化(3):用户个性化组件ProfileService
    查看>>
    PHP扩展-扩展的生成和编译
    查看>>
    CentOS 6安装详解及PXE 安装
    查看>>
    字符串的长度
    查看>>
    Android ListViewview入门
    查看>>
    DB2查询结果显示n行
    查看>>
    032-session函数
    查看>>
    抽象基类
    查看>>
    getopt实现传参自动识别
    查看>>
    韦东山驱动视频笔记——3.字符设备驱动程序之poll机制
    查看>>
    JavaScript-8(正则表达式)
    查看>>
    c++之调试帮助
    查看>>
    css(小笔记)
    查看>>