Ejemplo n.º 1
0
        public static AddressesManager ParseFromFile(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException($"{nameof(filePath)} should not be null");
            }

            string[] res = null;

            try
            {
                res = File.ReadAllLines(filePath);
            }
            catch (Exception)
            {
                throw new ApplicationException($"error while reading {filePath} file");
            }

            AddressesManager addrMngr = new AddressesManager();

            foreach (var line in res)
            {
                Address addr = GetAddressFromString(line);
                addrMngr.Addresses.Add(addr);
            }

            return(addrMngr);
        }
Ejemplo n.º 2
0
        public static void SerializeFromObjectToXmlFile(AddressesManager addressesManager, string filePath)
        {
            // TODO: fix - empty <parameters> tag added to the result file if there are no parameters
            // TODO: fix - empty <segments> tag added to the result file if there are no segments
            Encoding encoding = Encoding.GetEncoding("UTF-8");

            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

            ns.Add(string.Empty, string.Empty);

            XmlSerializer x = new XmlSerializer(typeof(AddressesManager));

            XmlWriterSettings xs = new XmlWriterSettings
            {
                Encoding         = Encoding.GetEncoding("UTF-8"),
                Indent           = true,
                IndentChars      = "\t",
                NewLineChars     = Environment.NewLine,
                ConformanceLevel = ConformanceLevel.Document
            };

            using (Stream s = File.OpenWrite(filePath))
            {
                // TODO: try to workaround the problem with empty tag written in short form
                // https://www.experts-exchange.com/questions/27641357/XmlWriter-Create-Empty-Tag-tag-tag.html
                // https://bytes.com/topic/net/answers/178893-force-xmlserializer-use-explicit-closing-tags-zero-length-strings
                // to force writing expanded empty tag
                x.Serialize(XmlWriter.Create(s, xs), addressesManager, ns);
            }
        }
Ejemplo n.º 3
0
        public static void Main(string[] args)
        {
            string _tempfileWithAddresses = @"c:\GitHub-RamanSmolsky\NET.S.2018\NET.S.2018.Smolsky.20\_tempAddresses.txt";
            string _filePathTemp          = @"c:\GitHub-RamanSmolsky\NET.S.2018\NET.S.2018.Smolsky.20\_temp.xml";

            AddressesManager addrMgr = Parser.ParseFromFile(_tempfileWithAddresses);

            Parser.SerializeFromObjectToXmlFile(addrMgr, _filePathTemp);

            Parser.DeserializeFromXmlFileToObject(_filePathTemp);
        }
Ejemplo n.º 4
0
        public static AddressesManager DeserializeFromXmlFileToObject(string filePath)
        {
            XmlSerializer x = new XmlSerializer(typeof(AddressesManager));

            AddressesManager res = null;

            using (Stream s = File.OpenRead(filePath))
            {
                res = (AddressesManager)x.Deserialize(s);
            }

            return(res);
        }