Home > front end >  Split multiline text into class objects C#
Split multiline text into class objects C#

Time:11-02

I have the following multiline text from an other application and I want to split it into multiple objects to create class objects. Does anybody have a good idea how to do it?

App name: Name1 
crown2-flow-engine: false 
read-protected: true 
priority: medium 
version: 1.0.0 
copy-protected: true 

App name: Name2
read-protected: true 
priority: medium 
version: 2.0.0 
copy-protected: true 

App name: Name3
crown2-flow-engine: false 
read-protected: true 
priority: medium 
version: 3.0.0 
copy-protected: true 

In the end I want to have something like:

  • Object1(name, priority, version)
  • Object2(name, priority, version)
  • Object3(name, priority, version)

I know thtat this can be done by splitting the text but I'm looking for a more generic solution.

I tried to do it with regex but I already failed to seperate the text into 3 blocks.

CodePudding user response:

Try following :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication49
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.txt";
        static void Main(string[] args)
        {
            StreamReader reader = new StreamReader(FILENAME);

            List<Application> applications = new List<Application>(); ;
            string line = "";
            Application application = null;
            while ((line = reader.ReadLine()) != null)
            {
                line = line.Trim();
                if (line.Length > 0)
                {
                    string[] splitArray = line.Split(new char[] { ':' });

                    switch (splitArray[0].Trim())
                    {
                        case "App name" :
                            application = new Application();
                            applications.Add(application);
                            application.name = splitArray[1].Trim();
                            break;
                        case "crown2-flow-engine":
                            application.flowEngine = splitArray[1].Trim() == "true" ? true : false;
                            break;
                        case "read-protected":
                            application.readProtected = splitArray[1].Trim() == "true" ? true : false;
                            break;
                        case "priority":
                            application.priority = splitArray[1].Trim();
                            break;
                        case "version":
                            application.version = splitArray[1].Trim();
                            break;
                        case "copy-protected":
                            application.copyProtected = splitArray[1].Trim() == "true" ? true : false;
                            break;
                    }

                }

            }
        }
    }
    public class Application
    {
        public string name { get; set; }
        public Boolean flowEngine { get; set; }
        public Boolean readProtected { get; set; }
        public string priority { get; set; }
        public string version { get; set; }
        public Boolean copyProtected { get; set; }
    }
}
  • Related