Click here to Skip to main content
15,888,270 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I got a requirement to create a windows service which gets a LDIF files from a folder and convert them to CSV files and keep it in another folder.

As I start with the implementation, I wrote the below code which by the way works fine.

VB
Dim dn As String = ""
Dim pa1 As String = ""
'Geting LDIF file
Dim Contents = IO.File.ReadAllLines("test_ldif.txt")

'Created a class called "ldap.vb" with all the tags that are there in the LDIF file 
'Created a object of it
Dim objLdap As New ldap()
Dim objLdapList As New List(Of ldap)()

For x As Integer = 0 To Contents.Count - 1
    If Not String.IsNullOrEmpty(Contents(x)) Then 'Omitting Spaces in the file
       pa1 = Contents(x).Split(":")(0) 'Getting the tag

       If pa1.Equals("dn") Then 'Checking for the matching tag and adding to the obj
           objLdapList.Add(objLdap) 'Adding Item to the list(DN is the Primary Key)
           objLdap = New [ldap]() 'Refreshing the obj

           dn = Contents(x).Split(":")(1)
           objLdap.dn = dn

       End If

       If pa1.Equals("employee") Then
           dn = Contents(x).Split(":")(1) 'Getting the value
           objLdap.employee = dn 'Adding it to the classObj
        End If

      If pa1.Equals("countryname") Then
           dn = Contents(x).Split(":")(1)
           objLdap.countryname = dn
           sb.AppendLine("<br/>countryname:" + dn)
      End If
.
.
.
.
.
.

    End If
Next
'Converting List object to CSV String
 Dim strnewCSV = GetCSV(objLdapList) 

'Saving Logic here...




The above code works good,

But the problem here is , if a new tag is added some time later to the LDIF, I need to update the class file and as well as the code in the for loop.
Is there any way to automate this.

I want a better solution to convert my LDIF file to CSV.


Thanks in advance.
Posted

1 solution

If you change your "schema" for your data objects (add remove properties) then you will have to recompile anyway.

You can create a "dynamic" importer but that would require more work and the necessity will depend on how often you change your schema, which I would not recommend.
 
Share this answer
 
Comments
praneeth arnepalli 4-Sep-15 3:10am    
Thanks for the reply.
SO , Is this the best approach if we don't change the schema such often?
Mehdi Gholam 4-Sep-15 4:12am    
If it works then it is good, if your requirements change frequently then you will have to do something else.

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