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 1010825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:08:32+00:00 2026-05-16T09:08:32+00:00

here is my problem I have the following array (for example) string[] arr =

  • 0

here is my problem

I have the following array (for example)

string[] arr = new[] { "s_0001", "s_0002", "s_0003", "sa_0004", "sa_0005", "sab_0006", "sab_0007" };

I want to do something that gives the following output

s_0001
sa_0004
sab_0006

I’ve tried everything but no luck! this will be the first step in a long project and any help would be most appreciated.

[edit] I don’t know when will the letters change, but I know that there will always be an underscore to separate the letters from the numbers. I need to somehow extract these letters, and then get rid of the duplicate ones

[edit] More specifically.. I wanna have unique entries of each string before the underscore, the numbers I don’t care about

[edit]
Ok guys! You’re really active I give you that. I didn’t expect I would get such quick answers. But as it seems (since I’ve been working on this for the last 8 hours) I’ve asked the wrong question

Here is my code

//Loop through the XML files in the Directory and get
//the objectName and GUID of each file
string[] arr_xmlFiles = Directory.GetFiles(Dir, "*.xml");   //Array with all XML Files in the Directory

foreach (string xmlFile in arr_xmlFiles)
{
    try
    {
        //Get the XMLs Name
        XDocument xmlF = XDocument.Load(xmlFile);
        string objectName = xmlF.Root.Name.ToString();

        //Get the XMLs GUID
        XElement oDcElement = xmlF.Root.FirstNode as XElement;
        Guid oGuid = new Guid(oDcElement.Attribute("DataclassId").Value);

        //Prints out the results 
        Console.WriteLine(" " + objectName + "    " + oGuid);
    }
    catch (XmlException) { }
}

What I’m doing basically is the following
I get all the XML files in a directory (They contain the ObjectName with its GUID)

i.e

CM_Commands [0ee2ab91-4971-4fd3-9752-cf47c8ba4a01].xml    
CM_Commands [1f627f72-ca7b-4b07-8f93-c5750612c209].xml

Sorry the breaking sign was ‘[‘ not ‘_’ but it doesn’t matter.

Now I save all these XMLs in an Array, then I wanna extract from these XMLs the ObjectName and the GUID for each one

After I do that I wanna do some modifications on only one of each XML that holds the same objectName

That’s all

  • 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-16T09:08:32+00:00Added an answer on May 16, 2026 at 9:08 am

    EDIT #3: detailed comments added to snippet below (see updated code under EDIT 2). Also note that if you want to return these from a method you’ll need to setup a new class with these properties, such as:

    public class MyClass 
    {
        public string ObjectName { get; set; }
        public string Guid { get; set; }
        public string FileName { get; set; }
    }
    

    With a class available, the select statement would change from select new { ... } to:

    /* start of query unchanged ... */
    select new MyClass
    {
        ObjectName = split[0],
        Guid = split[1],
        FileName = f.FullName
    };
    

    Your method, with all this code, would then have a return type of IEnumerable<MyClass>. You could easily change it to a List<MyClass> by using return results.ToList();.

    EDIT #2: to extract the objectName and Guid from your filename you don’t need to do all that tedious XML work to get the information from the internal details.

    Assuming your objectName and Guid are always separated by a space, you can use the following code. Otherwise more parsing (or, optionally, a regex) may be needed.

    string path = @"C:\Foo\Bar"; // your path goes here
    var dirInfo = new DirectoryInfo(path);
    
    // DirectoryInfo.GetFiles() returns an array of FileInfo[]
    // FileInfo's Name property gives us the file's name without the full path
    // LINQ let statement stores the split result, splitting the filename on spaces
    // and dots to get the objectName, and Guid separated from the file extension.
    // The "select new" projects the results into an anonymous type with the specified
    // properties and respectively assigned values. I stored the fullpath just in case.
    var query = from f in dirInfo.GetFiles("*.xml")
                let split = f.Name.Split(new[] { ' ', '.' })
                select new 
                {
                    ObjectName = split[0],
                    Guid = split[1],
                    FileName = f.FullName
                };
    
    // Now that the above query has neatly separated the ObjectName, we use LINQ
    // to group by ObjectName (the group key). Multiple files may exist under the same
    // key so we then select the First item from each group.
    var results = query.GroupBy(o => o.ObjectName)
                       .Select(g => g.First());
    
    // Iterate over the results using the projected property names.
    foreach (var item in results)
    {
        Console.WriteLine(item.FileName);
        Console.WriteLine("ObjectName: {0} -- Guid {1}", item.ObjectName, item.Guid);
    }
    

    This fits your sample data, however if you anticipate filenames with . characters the above will break. To remedy such a scenario change:

    1. The Split to: let split = f.Name.Split(' ')
    2. The Guid to: Guid = split[1].Substring(0, split[1].LastIndexOf('.')),

    Since you know there’ll always be an underscore you can try this approach:

    string[] arr = {"s_0001", "s_0002", "s_0003", "sa_0004", "sa_0005", "sab_0006", "sab_0007"};
    
    var query = arr.GroupBy(s => s.Substring(0, s.IndexOf('_')))
                   .Select(g => g.First());
    
    foreach (string s in query)
        Console.WriteLine(s);    // s_0001, sa_0004, sab_0006
    

    This will take the first item of each group so unless your items are pre-sorted, you may want to throw in an OrderBy in the Select: .Select(g => g.OrderBy(s => s).First());

    EDIT: in response to your edit, to get the distinct letters before the underscore (i.e., s, sa, sab) you can use the Enumerable.Distinct method as follows:

    var query = arr.Select(s => s.Substring(0, s.IndexOf('_')))
                   .Distinct();    // s, sa, sab
    

    That will give you an IEnumerable<string> that you can iterate over with a foreach as shown earlier.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

am new here. i have a slight problem; PLease look at the following code
I have the following problem: http://jsfiddle.net/DerNa/12/ Which is demonstrated more clearly the jump here:
Here is my problem: I have an array of model class(Let's say, 'addressModel' with
I'm new to python, and I have the following problem: I am trying to
I have problem adding arraylist to list view, will explain about my problem here..
First, sorry for my bad english, I'm French. Here the problem : I have
Here is the problem: I have two columns in a table that, for each
Here's the problem: I have a data-bound list of items, basically a way for
Here is my problem : I have a list of messages which I can
Here's my problem: I have do create a menu/list of actions (which would be

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.