- using System;
- using System.IO;
- using System.Xml;
- using System.Text;
- using System.Linq;
- using System.Xml.Serialization;
- using System.Collections.Generic;
- using System.Runtime.Serialization;
- using System.Runtime.Serialization.Formatters.Binary;
- namespace TlsCad.Collections
- {
- [Serializable]
- public class SerialList<TInfo, TValue> : SerialList<TValue>
- {
- public TInfo Info { get; set; }
- public SerialList()
- : base()
- { }
- public SerialList(IEnumerable<TValue> lst)
- : base(lst)
- { }
- public virtual void CopyFrom(SerialList<TInfo, TValue> lst)
- {
- Info = lst.Info;
- base.CopyFrom(lst);
- }
-
- #region Xml
- public new virtual void WriteXml(string path)
- {
- XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
- ns.Add("", "");
- using (XmlTextWriter writer = new XmlTextWriter(path, Encoding.UTF8))
- {
- writer.WriteStartDocument();
- writer.WriteStartElement("SerialList");
- writer.WriteStartElement("Info");
- XmlSerializer xs = new XmlSerializer(typeof(TInfo));
- xs.Serialize(writer, Info, ns);
- writer.WriteEndElement();
- writer.WriteStartElement("Values");
- xs = new XmlSerializer(typeof(TValue));
- foreach (TValue item in this)
- {
- xs.Serialize(writer, item, ns);
- }
- writer.WriteEndElement();
- writer.WriteEndElement();
- writer.Close();
- }
- }
- public new virtual SerialList<TInfo, TValue> ReadXml(string path)
- {
- if (File.Exists(path))
- {
- using (XmlTextReader reader = new XmlTextReader(path))
- {
- SerialList<TInfo, TValue> lst = new SerialList<TInfo, TValue>();
- reader.Normalization = false;
- reader.ReadToDescendant("Info");
- reader.Read();
- XmlSerializer xs = new XmlSerializer(typeof(TInfo));
- lst.Info = (TInfo)xs.Deserialize(reader);
- reader.Read();
- reader.Read();
- xs = new XmlSerializer(typeof(TValue));
- while (reader.NodeType != XmlNodeType.EndElement)
- {
- TValue item = (TValue)xs.Deserialize(reader);
- if (item != null)
- {
- lst.Add(item);
- }
- };
- reader.Close();
- CopyFrom(lst);
- return lst;
- }
- }
- return null;
- }
- #endregion
- #region Bin
- public new virtual SerialList<TInfo, TValue> ReadFrom(string path)
- {
- SerialList<TInfo, TValue> lst =
- (SerialList<TInfo, TValue>)base.ReadFrom(path);
- if (lst != null)
- {
- Info = lst.Info;
- }
- return lst;
- }
- #endregion
- }
- }
|