Working with a CSV file

I have been working and trying to solve this problem, maybe a whole week, and now I am wondering if I can solve it without diving even deeper into the C # language and I am fairly new to C # and also working with CSV files and sorting and organize them, so I'm pretty inexperienced in the whole spectrum of this.

I am trying to sort a CSV file alphabetically, hide the items to be hidden, and have them have depth levels based on their parent, child and grandchild items.

I have been successful with several of them and have written some working code, but I don't know how to sort them alphabetically and give them the correct depth layer based on the parent and child they belong to.

Here is the CSV array I was trying to organize:

ID;MenuName;ParentID;isHidden;LinkURL
1;Company;NULL;False;/company
2;About Us;1;False;/company/aboutus
3;Mission;1;False;/company/mission
4;Team;2;False;/company/aboutus/team
5;Client 2;10;False;/references/client2
6;Client 1;10;False;/references/client1
7;Client 4;10;True;/references/client4
8;Client 5;10;True;/references/client5
10;References;NULL;False;/references

      

I have separated the items with semicolons, I have displayed the items to be shown, but I cannot sort them as I should.

The sort should look like this:

Company
  About Us
    Team
  Mission
References
  Client 1
  Client 2

      

I tried to sort them or display them in that order by getting the slash index, but what the code reproduces is not how it should be displayed and it looks like this:

Company
  About Us
  Mission
    Team
  Client 2
  Client 1
References

      

In another attempt, where I recursively map my parent id to id, the console display looks like this:

Company
  About Us
  Mission
    Team
        Client 2
        Client 1
References

      

I tried to solve this with a friend and even he doesn't know how to approach this problem as this code should work with a different file that uses different parent IDs.

On top of all this, I cannot index them into an array because only index 0 or index is based on their emails or the console crashes if I enter index position 1.

Here's the code for the first part where I don't sort them:

class Program
{
    static void Main(string[] args)
    {
        StreamReader sr = new StreamReader(@"Navigation.csv");
        string data = sr.ReadLine();

        while (data != null)
        {
            string[] rows = data.Split(';');
            int id;
            int parentId;
            bool ids = Int32.TryParse(rows[0], out id);
            string name = rows[1];
            bool pIds = Int32.TryParse(rows[2], out parentId);
            string isHidden = rows[3];
            string linkUrl = rows[4];
            string[] splitted = linkUrl.Split('/');

            if (isHidden == "False")
            {
                List<CsvParentChild> pIdCid = new List<CsvParentChild>()
                {
                    new CsvParentChild(id, parentId, name, linkUrl)
                };
            }

            data = sr.ReadLine();
        }
    }
}

class CsvParentChild
{

    public int Id;
    public int ParentId;
    public string Name;
    public string LinkUrl;
    public List<CsvParentChild> Children = new List<CsvParentChild>();

    public CsvParentChild(int id, int parentId, string name, string linkUrl)
    {

        Id = id;
        ParentId = parentId;
        Name = name;
        LinkUrl = linkUrl;
        string[] splitted = linkUrl.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

        if (splitted.Length == 1)
        {
            Console.WriteLine($". { name }");
        }
        else if (splitted.Length == 2)
        {
            Console.WriteLine($".... { name }");
        }
        else if (splitted.Length == 3)
        {
            Console.WriteLine($"....... { name }");
        }
    }
}

      

But for the second part:

class Program
{
    static void Main(string[] args)
    {
        // Get the path for the file
        const string filePath = @"../../Navigation.csv";

        // Read the file
        StreamReader sr = new StreamReader(File.OpenRead(filePath));
        string data = sr.ReadLine();

        while (data != null)
        {

            string[] rows = data.Split(';');

            ListItems lis = new ListItems();

            int id;
            int parentId;

            // Get the rows/columns from the Csv file
            bool ids = Int32.TryParse(rows[0], out id);
            string name = rows[1];
            bool parentIds = Int32.TryParse(rows[2], out parentId);
            string isHidden = rows[3];
            string linkUrl = rows[4];

            // Split the linkUrl so that we get the position of the
            // elements based on their slash
            string [] splitted = linkUrl.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            // If item.isHidden == "False"
            // then display the all items whose state is set to false.
            // If the item.isHidden == "True", then display the item
            // whose state is set to true.
            if (isHidden == "False")
            {
                // Set the items
                ListItems.data = new List<ListItems>()
                {
                    new ListItems() { Id = id, Name = name, ParentId = parentId },
                };

                // Make a new instance of ListItems()
                ListItems listItems = new ListItems();

                // Loop through the CSV data
                for (var i = 0; i < data.Count(); i++)
                {
                    if (splitted.Length == 1)
                    {
                        listItems.ListThroughItems(i, i);
                    }
                    else if (splitted.Length == 2)
                    {
                        listItems.ListThroughItems(i, i);
                    }
                    else
                    {
                        listItems.ListThroughItems(i, i);
                    }
                }
            }

            // Break out of infinite loop
            data = sr.ReadLine();
        }
    }

public class ListItems
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int ParentId { get; set; }
        public static List<ListItems> data = null;
        public List<ListItems> Children = new List<ListItems>();

        // http://stackoverflow.com/a/36250045/7826856
        public void ListThroughItems(int id, int level)
        {
            Id = id;

            // Match the parent id with the id
            List<ListItems> children = data
                .Where(p => p.ParentId == id)
                .ToList();

            foreach (ListItems child in children)
            {
                string depth = new string('.', level * 4);
                Console.WriteLine($".{ depth } { child.Name }");
                ListThroughItems(child.Id, level + 1);
            }
        }
    }
}

      

+3


source to share


2 answers


For each element, you need to build some sort of "sort array" of identifiers. The sort array consists of ancestor IDs in order from most distant to least distant. For "Team" our sorting array [1, 2, 4]

.

Here are the sort arrays for each item:

[1]
[1, 2]
[1, 3]
[1, 2, 4]
[10, 5]
[10, 6]
[10, 7]
[10, 8]
[10]

      

Once you've done that, sorting the items is straightforward. When comparing two sort arrays, start with the numbers in order in each array. If they are different, collect according to the value of the first number, and you're done. If they are the same, look at the second number. If there is no second number, then sort by the length of the arrays, i.e. Nothing happens before something.



Applying this algorithm, we get:

[1]
[1, 2]
[1, 2, 4]
[1, 3]
[10]
[10, 5]
[10, 6]
[10, 7]
[10, 8]

      

After that, hide the elements based on the flag. I leave it to you because it is that simple. The depth is simple: the length of the sort array.

+1


source


My application was compiled and produced the following data with your data:

Company
        About Us
                Team
        Mission
References
        Client 1
        Client 2
        Client 4
        Client 5

      

I would try to use an object relation to create your tree type structure. The main problem is that parents don't matter. Children do. Therefore, at some point in your code, you will need to reverse the hierarchy; Analyze the children first, but read their Parents first to create an exit.

The roots of our tree are data records without parents.

Syntactic

This should be pretty clear, we have a class with a constructor that parses the input array and stores the data in it. We keep all the lines in the list. After we were done with that, we pretty much transformed the list, but no sorting happened at all.

public partial class csvRow
{
    // Your Data
    public int Id { get; private set; }
    public string MenuName { get; private set; }
    public int? ParentId { get; private set; }
    public bool isHidden { get; private set; }
    public string LinkURL { get; private set; }

    public csvRow(string[] arr)
    {
        Id = Int32.Parse(arr[0]);
        MenuName = arr[1];
        //Parent Id can be null!
        ParentId = ToNullableInt(arr[2]);
        isHidden = bool.Parse(arr[3]);
        LinkURL = arr[4];
    }
    private static int? ToNullableInt(string s)
    {
        int i;
        if (int.TryParse(s, out i))
            return i;
        else
            return null;
    }
}
static void Main(string[] args)
{
    List<csvRow> unsortedRows = new List<csvRow>();
    // Read the file
    const string filePath = @"Navigation.csv";
    StreamReader sr = new StreamReader(File.OpenRead(filePath));
    string data = sr.ReadLine();
    //Read each line
    while (data != null)
    {
        var dataSplit = data.Split(';');   
        //We need to avoid parsing the first line. 
        if (dataSplit[0] != "ID" )
        {
            csvRow lis = new csvRow(dataSplit);
            unsortedRows.Add(lis);
        }
         // Break out of infinite loop
         data = sr.ReadLine();
      }
      sr.Dispose();

       //At this point we got our data in our List<csvRow> unsortedRows
       //It parsed nicely. But we still need to sort it.
       //So let get ourselves the root values. Those are the data entries that don't have a parent.
       //Please Note that the main method continues afterwards.

      

Creating our tree structure and sorting the elements

Let's start by defining children and a public property ChildrenSorted, which returns them sorted. That's really all we do, it's much easier to sort than recursively.

We also need a function to add children. It will filter the input pretty hard and find all rows where row.parentId = this.ID.

The latter is a function that defines our output and allows us to get something that we can print to the console.



public partial class csvRow
{
    private List<csvRow> children = new List<csvRow>();
    public List<csvRow> ChildrenSorted
    {
        get
        {
            // This is a quite neet way of sorting, isn't it?
            //Btw this is all the sorting we are doing, recursion for win!
            return children.OrderBy(row => row.MenuName).ToList();
        }
    }

    public void addChildrenFrom(List<csvRow> unsortedRows)
    {
        // Add only rows where this is the parent.
        this.children.AddRange(unsortedRows.Where(
            //Avoid running into null errors
            row => row.ParentId.HasValue &&
            //Find actualy children
            row.ParentId == this.Id &&
            //Avoid adding a child twice. This shouldn't be a problem with your data,
            //but why not be careful?
            !this.children.Any(child => child.Id == row.Id)));  


        //And this is where the magic happens. We are doing this recursively.
        foreach (csvRow child in this.children)
        {
            child.addChildrenFrom(unsortedRows);
        }
    }

    //Depending on your use case this function should be replaced with something
    //that actually makes sense for your business logic, it an example on
    //how to read from a recursiv structure.
    public List<string> FamilyTree
    {
        get
        {
            List<string> myFamily = new List<string>();
            myFamily.Add(this.MenuName);
            //Merges the Trees with itself as root.
            foreach (csvRow child in this.ChildrenSorted)
            {
                foreach (string familyMember in child.FamilyTree)
                {
                    //Adds a tab for all children, grandchildren etc.
                    myFamily.Add("\t" + familyMember);
                }
            }
            return myFamily;
        }
    }
}

      

Adding and Accessing Items to the Tree

This is the second part of my main function, where we are actually working with our data (right after sr.Dispose ();)

    var roots = unsortedRows.Where(row => row.ParentId.HasValue == false).
        OrderBy(root => root.MenuName).ToList();

    foreach (csvRow root in roots)
    {
        root.addChildrenFrom(unsortedRows);
    }

    foreach (csvRow root in roots)
    {
        foreach (string FamilyMember in root.FamilyTree)
        {
            Console.WriteLine(FamilyMember);
        }
    }
    Console.Read();
}

      

Whole Source Code (Visual Studio C # Console Application)

You can use this to test, play around, and learn more about recursive structures.

Copyright 2017 Eldar Kersebaum

Apache License Version 2.0 ("License"); you may not use this file except in accordance with the License. You can get a copy of the License for

 http://www.apache.org/licenses/LICENSE-2.0

      

Unless provided by applicable law or agreed in writing, the software distributed under the license is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied. See the License in a specific language for permissions and limitations of the License.

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

namespace ConsoleApplication49
    {
    class Program
    {
        static void Main(string[] args)
        {
            List<csvRow> unsortedRows = new List<csvRow>();
            const string filePath = @"Navigation.csv";
            StreamReader sr = new StreamReader(File.OpenRead(filePath));
            string data = sr.ReadLine();
            while (data != null)
            {
                var dataSplit = data.Split(';');   
                //We need to avoid parsing the first line. 
                if (dataSplit[0] != "ID" )
                {
                    csvRow lis = new csvRow(dataSplit);
                    unsortedRows.Add(lis);
                }
                // Break out of infinite loop
                data = sr.ReadLine();
            }
            sr.Dispose();
            var roots = unsortedRows.Where(row => row.ParentId.HasValue == false).
                OrderBy(root => root.MenuName).ToList();

            foreach (csvRow root in roots)
            {
                root.addChildrenFrom(unsortedRows);
            }

            foreach (csvRow root in roots)
            {
                foreach (string FamilyMember in root.FamilyTree)
                {
                    Console.WriteLine(FamilyMember);
                }
            }
            Console.Read();
        }
    }
    public partial class csvRow
    {
        // Your Data
        public int Id { get; private set; }
        public string MenuName { get; private set; }
        public int? ParentId { get; private set; }
        public bool isHidden { get; private set; }
        public string LinkURL { get; private set; }

        public csvRow(string[] arr)
        {
            Id = Int32.Parse(arr[0]);
            MenuName = arr[1];
            ParentId = ToNullableInt(arr[2]);
            isHidden = bool.Parse(arr[3]);
            LinkURL = arr[4];
        }
        private static int? ToNullableInt(string s)
        {
            int i;
            if (int.TryParse(s, out i))
                return i;
            else
                return null;
        }
        private List<csvRow> children = new List<csvRow>();
        public List<csvRow> ChildrenSorted
        {
            get
            {
                return children.OrderBy(row => row.MenuName).ToList();
            }
        }
        public void addChildrenFrom(List<csvRow> unsortedRows)
        {
            this.children.AddRange(unsortedRows.Where(
                row => row.ParentId.HasValue &&
                row.ParentId == this.Id &&
                !this.children.Any(child => child.Id == row.Id)));
            foreach (csvRow child in this.children)
            {
                child.addChildrenFrom(unsortedRows);
            }
        }
        public List<string> FamilyTree
        {
            get
            {
                List<string> myFamily = new List<string>();
                myFamily.Add(this.MenuName);
                foreach (csvRow child in this.ChildrenSorted)
                {
                    foreach (string familyMember in child.FamilyTree)
                    {
                        myFamily.Add("\t" + familyMember);
                    }
                }
                return myFamily;
            }
        }
    }
}

      

+1


source







All Articles