[Serializable] public class CardItemInfo { private int lineWidth;//线宽 private CardItemInfo childCardItemInfo; public int LineWidth { get { return lineWidth; } set { lineWidth = value; } } public int ChildCardItemInfo { get { return childCardItemInfo; } set { childCardItemInfo = value; } } }
定义列表
private static List<CardItemInfo> listItemInfo=new List<CardItemInfo>();
序列化到XML
public static bool SaveXml() { CardItemInfo cardItemInfo=new CardItemInfo(); cardItemInfo.LineWidth = 11; CardItemInfo childCardItemInfo=new CardItemInfo(); childCardItemInfo.LineWidth = 22; cardItemInfo.ChildCardItemInfo = childCardItemInfo; listItemInfo.Add(cardItemInfo); Stream fStream = new FileStream(System.Windows.Forms.Application.StartupPath + @" est11.xml", FileMode.Create); XmlSerializer xmlFormat = new XmlSerializer(typeof(List<CardItemInfo>)); xmlFormat.Serialize(fStream, listItemInfo);//序列化对象 return false; }
从XML反序列化
public static bool LoadFromXml(String fileName) { try { System.IO.Stream fStream = new FileStream(fileName, FileMode.Open); XmlSerializer xmlFormat = new XmlSerializer(typeof(List<CardItemInfo>)); listItemInfo = (List<CardItemInfo>)xmlFormat.Deserialize(fStream); fStream.Close(); MessageBox.Show("OK"); } catch (Exception e) { MessageBox.Show(e.Message); return false; } return true; }
博客资源:QQ群616945527,博客相关资源,同名文件。