Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3223800
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:08:56+00:00 2026-05-17T16:08:56+00:00

UPDATE: I did not realise that Thomas Levesque had posted a solution and worked

  • 0

UPDATE:

I did not realise that Thomas Levesque had posted a solution and worked on my own below one in the meantime. I’m not using C# / VS2010 for long but I must say that C# is unbelievably productive and I am kicking myself that I stuck with C++ and put off learning C# for so long.

 Stack<string> LdifUserStack = new Stack<string>();
          String LdifValidUser = null;

            var query2 = File.ReadAllLines(args[1]).ToList();

            foreach (var item in query2)
            {

                if ( item.Contains("Fullname") ) continue;

                LdifValidUser += item + System.Environment.NewLine;

                if ( LdifValidUser.Contains("GivenName:") && LdifValidUser.Contains("SN:") )
                {
                    LdifUserStack.Push(LdifValidUser);
                    LdifValidUser = null;
                }

                else if ( string.IsNullOrEmpty(item) )
                {
                    LdifValidUser = null;
                }

            }

END UPDATE:

I have a simple ldif file and I am trying to split on /r/n/r/n but no joy, whatever combination i use the split always occurs on each line instead.

  var query2 =

           from line in File.ReadAllLines(args[1])
           let LDIFRecord = line.Split(new string[] { "\r\n\r\n"},StringSplitOptions.None) 
           select LDIFRecord;

            foreach (var item in query2)
            {
                //do something


            }

 //Also tried line.Split(new string[] {"'\r\n','\r\n'"},StringSplitOptions.None)
 // sample ldif file
    dn: cn=Admin1,ou=abt,o=cach
    changetype: modify
    GivenName: Admin
    Fullname: Administrator for abt Server
    SN: Admin
    CN: Admin

    dn: cn=admin,ou=ters,o=cach
    changetype: modify
    GivenName: Administrator
    Fullname: cach Administrator
    SN: admin
    CN: admin

    dn: cn=Supervisor,o=cach
    changetype: modify
    SN: Supervisor
    CN: Supervisor
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:08:57+00:00Added an answer on May 17, 2026 at 4:08 pm

    File.ReadAllLines already splits the file into lines, so the individual lines don’t contain “\r\n” sequences… What are you trying to do exactly ?


    EDIT: OK, here’s a solution

    In your files, records are separated by a blank line. File.ReadAllLines returns all lines from the files, but doesn’t separate the records. So you just need to “split” the array of lines based on blank lines.

    Here’s an extension method to split a sequence of items based on an arbitrary criteria:

    public static class Extensions
    {
        public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> source, Func<T, bool> isSeparator)
        {
            var list = new List<T>();
            foreach (var item in source)
            {
                if (isSeparator(item))
                {
                    if (list.Count > 0)
                    {
                        yield return list.AsReadOnly();
                    }
                    list = new List<T>();
                }
                else
                {
                    list.Add(item);
                }
            }
            if (list.Count > 0)
            {
                yield return list.AsReadOnly();
            }
        }
    }
    

    In your case, you can use it like this:

    var records = File.ReadAllLines(args[1]).Split(line => String.IsNullOrWhiteSpace(line));
    

    Which can be shortened to:

    var records = File.ReadAllLines(args[1]).Split(String.IsNullOrWhiteSpace);
    

    Now, if you want to filter the records to keep only the ones that have the GivenName and SN attributes, you just have to use Where:

    var records = File.ReadAllLines(args[1])
                      .Split(String.IsNullOrWhiteSpace)
                      .Where(rec => rec.Any(s => s.StartsWith("GivenName:")
                                 && rec.Any(s => s.StartsWith("SN:"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update: Solved, with code I got it working, see my answer below for the
Update : Looks like the query does not throw any timeout. The connection is
Update: Now that it's 2016 I'd use PowerShell for this unless there's a really
Update: if you've added symbolic breakpoints and they're not working, uh, wait awhile. After
Now I realise this topic has been covered many times before. However there did
Update: Check out this follow-up question: Gem Update on Windows - is it broken?
Update: giving a much more thorough example. The first two solutions offered were right
UPDATE: Focus your answers on hardware solutions please. What hardware/tools/add-in are you using to
UPDATE - A comprehensive comparison, updated as of February 2015, can be found here:
UPDATE: Thanks to everyone for the responses. I didn't realize document.write() was deprecated. Add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.