예제 #1
0
        public virtual void SaveFile <T>(SettingFileAttribute attr, object target)
        {
            string filePath   = attr.GetFilePath();
            Type   rootType   = attr.ContainerType ?? typeof(T);
            var    serializer = new DataContractSerializer(rootType, rootType.Name, string.Empty);

            using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
                using (var writer = XmlWriter.Create(stream,
                                                     new XmlWriterSettings()
                {
                    Encoding = Encoding.GetEncoding(932)
                }))
                {
                    serializer.WriteObject(writer, target);
                }
        }
예제 #2
0
        public virtual IList <T> ReadFile <T>(SettingFileAttribute attr)
            where T : class, IEquatable <T>, new()
        {
            string filePath = attr.GetFilePath();

            if (!System.IO.File.Exists(filePath))
            {
                return(null);
            }

            var serializer = new DataContractSerializer(attr.ContainerType ?? typeof(T));

            using (var reader = XmlReader.Create(filePath))
            {
                if (attr.ContainerType == null)
                {
                    return(new T[] { serializer.ReadObject(reader) as T });
                }
                else
                {
                    return(serializer.ReadObject(reader) as IList <T>);
                }
            }
        }