Best way to store 2-3 parameters per line, separated by commas.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Best way to store 2-3 parameters per line, separated by commas.

  1. #1
    Join Date
    May 2012
    Posts
    25

    Best way to store 2-3 parameters per line, separated by commas.

    Hello,

    I'm working on a project, where I need to store 2 to 3 parameters per line, and reading them back out one line at a time.

    Server1,Name,Path
    Server2,Name,Path
    Server3,Name,Path

    Then, read these back out.

    I'm doing it in XML now and having troubles, getting a specific value out and thought that csv file would be easier.

    Thanks for your Help.

  2. #2
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,196

    Re: Best way to store 2-3 parameters per line, separated by commas.

    I believe xml is simpler if you use XmlSerialization. Consider where you need to store a list of servers. Since you specified Server1, Server2, and so on, let's make them indexed.

    The xml schema may look something like this:
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <servers>
      <server index="1" name="nameofserver1" path="pathofserver1"/>
      <server index="2" name="nameofserver2" path="pathofserver2"/>
      <server index="3" name="nameofserver3" path="pathofserver3"/>
    </servers>
    Then we write a couple of classes to read/write the xml:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;
    
    namespace CG.XmlServerTest
    {
    
        /// <summary>
        /// Wrapper class to read/write servers.xml (TODO: add write functionality(
        /// </summary>
        [Serializable]
        [XmlType(AnonymousType = true, Namespace = "")]
        [XmlRoot(ElementName = "servers", Namespace = "", IsNullable = false)]
        public class ServerMgr
        {
            public static ServerMgr Open(string xmlFile)
            {
                var serializer = new XmlSerializer(typeof(ServerMgr));
    
                using (var reader = XmlReader.Create(xmlFile))
                {
                    return (ServerMgr)serializer.Deserialize(reader, "");
                }
    
            }
    
            public ServerMgr()
            {
                _servers = new Dictionary<int, Server>();
            }
    
            /// <summary>
            /// Retrieves a Server by index
            /// </summary>
            public Server this[int index]
            {
                get
                {
                    return _servers[index];
                }
            }
    
            /// <summary>
            /// Adds a new server entry
            /// </summary>
            public void Add(string name, string path)
            {
                var newIndex = _servers.Count;
                _servers.Add(newIndex, new Server { Index = newIndex, Name = name, Path = path });
            }
    
            /// <remarks/>
            /// <remarks/>
            [XmlElement("server")]
            public Server[] Servers
            {
                get
                {
                    return _servers.Values.ToArray();
                }
                set
                {
                    if (value != null)
                    {
                        foreach (var s in value)
                        {
                            _servers.Add(s.Index, s);
                        }
                    }
                }
            }
    
            private Dictionary<int, Server> _servers = new Dictionary<int, Server>();
        }
    
        /// <summary>
        /// Represents a single server
        /// </summary>
        public class Server
        {
            [XmlAttribute("index")]
            public int Index { get; set; }
            [XmlAttribute("name")]
            public string Name { get; set; }
            [XmlAttribute("path")]
            public string Path { get; set; }
    
            public override string ToString()
            {
                return String.Format("Index: {0,-10} Name: {1,-25} Path: {2}", Index, Name, Path);
            }
        }
    }
    Finally, we use the new classes:
    Code:
    using System;
    
    namespace CG.XmlServerTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                var serverMgr = ServerMgr.Open("servers.xml");
    
                foreach (var server in serverMgr.Servers)
                {
                    Console.WriteLine(server);
                }
            }
        }
    }
    Output:
    Code:
    Index: 1          Name: nameofserver1             Path: pathofserver1
    Index: 2          Name: nameofserver2             Path: pathofserver2
    Index: 3          Name: nameofserver3             Path: pathofserver3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center