标签:
一。 用法说明
[XmlRootAttribute("BASEINFO")] // 当该类为Xml根节点时,以此为根节点名称。 public class BASEINFO
[XmlAttribute("IDCARD")] // 表现为Xml节点属性。<... IDCARD="..."/> public string IDCARD
[XmlElementAttribute("NAME", IsNullable = false)] // 表现为Xml节点。<NAME>...</NAME> public string NAME
[XmlElementAttribute("Area")] // 表现为水平结构的Xml节点。<Area ... /><Area ... />public List<Area> data
[XmlArrayAttribute("OPERATIONS", IsNullable = false)] // 表现为Xml层次结构,根为OPERATIONS,其所属的每个该集合节点元素名为类名。<OPERATIONS><OPERATION>... </OPERATION></OPERATIONS>
public List<OPERATION> data
[XmlIgnoreAttribute] // 忽略该元素的序列化。
二。代码
<V1>
<OPERATIONS>
<OPERATION>
<NAME>oo</NAME>
<DATE>2015/10/16 0:00:00</DATE>
</OPERATION>
<OPERATION>
<NAME></NAME>
<DATE>2015/10/16 0:00:00</DATE>
</OPERATION>
</OPERATIONS>
</V1>
对应Model, Model定义好后可以对其进行序列化及反序列化
public class V1 {
private List<OPERATION> data = new List<OPERATION>();
[XmlArray("OPERATIONS")]
[XmlArrayItem("OPERATION")]
public List<OPERATION> Pdata
{
get { return data; }
}
}
三。反序列化:
/// <summary> /// xml格式的字串反序列化为指定Object /// </summary> /// <typeparam name="T"></typeparam> /// <param name="data"></param> /// <returns></returns> public static T XmlDeserialize<T>(string data) { XmlDocument xdoc = new XmlDocument(); try { xdoc.LoadXml(data); XmlNodeReader reader = new XmlNodeReader(xdoc.DocumentElement); XmlSerializer ser = new XmlSerializer(typeof(T)); object obj = ser.Deserialize(reader); return (T)obj; } catch (Exception e) { return default(T); } } #endregion
参考URL:
https://www.mgenware.com/blog/?p=142
http://blog.csdn.net/philosophyatmath/article/details/28593153
标签:
原文地址:http://www.cnblogs.com/kittyguo/p/4891388.html