Click here to Skip to main content
15,877,145 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a program that reads that from a server. The data returned consists of a multiline string. My requirement is to cast the containing substrings (for all the lines) to a Model then later save to a database table.

This is a sample of my data :
C#
Filesystem           1024-blocks        Used   Available Capacity  Mounted on
rpool/ROOT/solaris-0    47185886    39055271     6723316    86%    /
rpool/ROOT/solaris-0/var    47185886      334036     6723316     5%    /var
/dev                           0           0           0     0%    /dev
/u01                   104857600    92783432    12074167    89%    /u01
proc                           0           0           0     0%    /proc
ctfs                           0           0           0     0%    /system/contract


And for each line i want to cast all the containing substrings to the Model 's properties. The model (Weblogic.cs) is as below :
C#
public String Filesystem { get; set; }

       public string Blocks { get; set; }

       public string Used { get; set; }

       public string Available { get; set; }

       public string Capacity { get; set; }

       public string Mount { get; set; }


For now i have made all model properties strings and will do the conversion when saving the data.

My problem is that my logic only returns data from the first line i.e when i step through the code the servers property has Count 1:

C#
<pre> using (SshClient ssh = new SshClient("+ server +",
                "+username+", "+password+"))
                {
                    ssh.Connect();
                    var result = ssh.RunCommand("df -k");
                    var rss = result.Result;
                    string[] lines = rss.Split('\n');
                    lines = lines.Skip(1).ToArray();
                    Int32 count = 6;
                    String[] separator = { " " };

                    var servers = new List<Weblogic>();


                    List<string> infos = new List<string>();
                    for (int i = 0; i < lines.Length; i++)
                    {
                        String[] strlist = lines[i].Split(separator, count,
                        StringSplitOptions.RemoveEmptyEntries);

                      
                            foreach (var value in strlist)
                            {

                                Weblogic ci = new Weblogic();

                                ci.Filesystem = strlist[0];
                                ci.Blocks = strlist[1];
                                ci.Used = strlist[2];
                                ci.Available = strlist[3];
                                ci.Capacity = strlist[4];
                                ci.Mount = strlist[5];

                                servers.Add(ci);
                               }
					    					   
							   
				}
							   
		  }


What I have tried:

When i try using this approach i am able to grab all substrings from all the lines :
C#
<pre>  using (SshClient ssh = new SshClient("+server+",
                "+username+", "+password+"))
                {
                    ssh.Connect();
                    var result = ssh.RunCommand("df -k");
                    var rss = result.Result;
                    string[] lines = rss.Split('\n');
                    lines = lines.Skip(1).ToArray();
                    Int32 countt = 6;
                    String[] separator = { " " };

                    var servers = new List<Weblogic>();
                 
                    for (int i = 0; i < lines.Length; i++)
                    {
                        String[] strlist = lines[i].Split(separator, countt,
                        StringSplitOptions.RemoveEmptyEntries);

                        for (int j = 0; j < strlist.Length; j++)
                        {
						 string s = strlist[j];
                            Console.WriteLine(s);
                            Console.ReadKey();
                                                     
                        }

                    }
		}
Posted
Updated 9-Jun-20 1:18am
Comments
TheRealSteveJudge 9-Jun-20 6:53am    
When debugging, what does the 'lines' object look like?
Is it an array? If yes, how many entries does it have?
Tshumore 9-Jun-20 7:47am    
Its an array and contains 24 elements which is in actual case the whole dataset. The problem however is when i read and add them to servers object. Im only getting 1
TheRealSteveJudge 9-Jun-20 7:52am    
OK. Please have a look at solutions 2 and 3...
Tshumore 9-Jun-20 8:07am    
I have amended the code as in the solutions. The problem is at String[] strlist . strlist returns only 6 substrings - actually only the 6 substrings for the first line. Its not repeating for each returned line. Unless im missing something very obvious i think this is where the flow is failing

We can't access exactly the data you are working with - OK, you've shown a sample, but we can't tell what it contains: for example spaces and tabs look identical, lines can have multiple terminator characters rather than just "\n", and so forth.

And data is fundamental to this problem.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
v2
Your inner loop is not correct and not needed, try this:
C#
// this line is not needed either : List<string> infos = new List<string>();
for (int i = 0; i < lines.Length; i++) // repeat for each returned line
{
    // split the line into 6 fields
    String[] strlist = lines[i].Split(separator, count,
    StringSplitOptions.RemoveEmptyEntries);
    
    // create a new Weblogic objuect 
    Weblogic ci = new Weblogic();
    
    // add the detail fields
    ci.Filesystem = strlist[0];
    ci.Blocks = strlist[1];
    ci.Used = strlist[2];
    ci.Available = strlist[3];
    ci.Capacity = strlist[4];
    ci.Mount = strlist[5];
    
    // add this object to the servers list
    servers.Add(ci);
}
 
Share this answer
 
Comments
Tshumore 9-Jun-20 7:53am    
The problem is at String[] strlist . strlist returns only 6 substrings - actually 6 substrings for the first line only. Its not repeating for each returned line.
Richard MacCutchan 9-Jun-20 8:24am    
You need to use the debugger to find out why. The loop looks correct as it used lines.Length to get the number of lines in the array.
Have a look at
foreach (var value in strlist)
You never use the variable 'value'.

You must replace
C#
foreach (var value in strlist)
{
	Weblogic ci = new Weblogic();

	ci.Filesystem = strlist[0];
	ci.Blocks = strlist[1];
	ci.Used = strlist[2];
	ci.Available = strlist[3];
	ci.Capacity = strlist[4];
	ci.Mount = strlist[5];

	servers.Add(ci);
}
by
C#
Weblogic ci = new Weblogic();

ci.Filesystem = strlist[0];
ci.Blocks = strlist[1];
ci.Used = strlist[2];
ci.Available = strlist[3];
ci.Capacity = strlist[4];
ci.Mount = strlist[5];

servers.Add(ci);
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900