示例,主要包括System.Xml.Serialization命名空间下的XmlRoot、XmlElement、XmlAttribute、XmlText、XmlIgnore等特性的简单使用,高级使用可自行查看msdn。
实体类代码:
[XmlRoot("信息")] // 该特性标记为根节点 public class Info { [XmlIgnore] // 此公共属性不会被序列化 public int Count { get; set; } public Server 服务端 { get; set; } // 不使用标记 [XmlElement("客户端")] // List<Client> 表示有多个 '客户端' 节点 public List<Client> Client { get; set; } } public class Server { [XmlAttribute("备注")] // 节点 '服务端' 的一个名为 '备注' 的属性 public string Note; [XmlText] // 节点 '服务端' 的值 public string Name; } public class Client { [XmlAttribute("名称")] // 在 '客户端' 节点添加名为 '名称' 的属性 public string Name { get; set; } [XmlElement("地址")] // 节点 '客户端' 的子节点 public string Adress { get; set; } [XmlElement("端口")] // 节点 '客户端' 的子节点 public string Port { get; set; } }