Where to store constant objects that will be used in my application

前端 未结 5 2070
[愿得一人]
[愿得一人] 2021-01-23 05:00

Lets say I have an item, which has fields(properties)

  1. Location
  2. Average value
  3. Usability

And I have 10-15 items

5条回答
  •  甜味超标
    2021-01-23 05:47

    You can serialize and deserialize a List to and from an XML file using this helper class:

    public static class XmlHelper
    {
        // Specifies whether XML attributes each appear on their own line
        const bool newLineOnAttributes = false;
    
        public static bool NewLineOnAttributes { get; set; }
        /// 
        /// Serializes an object to an XML string, using the specified namespaces.
        /// 
        public static string ToXml(object obj, XmlSerializerNamespaces ns)
        {
            Type T = obj.GetType();
    
            var xs = new XmlSerializer(T);
            var ws = new XmlWriterSettings { Indent = true, NewLineOnAttributes = newLineOnAttributes, OmitXmlDeclaration = true };
    
            var sb = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(sb, ws))
            {
                xs.Serialize(writer, obj, ns);
            }
            return sb.ToString();
        }
    
        /// 
        /// Serializes an object to an XML string.
        /// 
        public static string ToXml(object obj)
        {
            var ns = new XmlSerializerNamespaces();
            ns.Add("", "");
            return ToXml(obj, ns);
        }
    
        /// 
        /// Deserializes an object from an XML string.
        /// 
        public static T FromXml(string xml)
        {
            XmlSerializer xs = new XmlSerializer(typeof(T));
            using (StringReader sr = new StringReader(xml))
            {
                return (T)xs.Deserialize(sr);
            }
        }
    
        /// 
        /// Serializes an object to an XML file.
        /// 
        public static void ToXmlFile(Object obj, string filePath)
        {
            var xs = new XmlSerializer(obj.GetType());
            var ns = new XmlSerializerNamespaces();
            var ws = new XmlWriterSettings { Indent = true, NewLineOnAttributes = NewLineOnAttributes, OmitXmlDeclaration = true };
            ns.Add("", "");
    
            using (XmlWriter writer = XmlWriter.Create(filePath, ws))
            {
                xs.Serialize(writer, obj);
            }
        }
    
        /// 
        /// Deserializes an object from an XML file.
        /// 
        public static T FromXmlFile(string filePath)
        {
            StreamReader sr = new StreamReader(filePath);
            try
            {
                var result = FromXml(sr.ReadToEnd());
                return result;
            }
            catch (Exception e)
            {
                throw new Exception(e.InnerException.Message);
            }
            finally
            {
                sr.Close();
            }
        }
    }
    

    Usage:

    XmlHelper.ToXmlFile(myList, @"c:\folder\file.xml");
    
    var list = XmlHelper.FromXmlFile>(@"c:\folder\file.xml");
    

提交回复
热议问题