Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4.  
  5. public class Folders
  6. {
  7.     public static IEnumerable<string> FolderNames(string xml, char startingLetter)
  8.     {
  9.         List<string> result = new List<string>();
  10.  
  11.         char[] separator = {'"'};
  12.  
  13.         string[] wordsArray = xml.Split(separator, StringSplitOptions.RemoveEmptyEntries);
  14.  
  15.         for (int i = 0; i < wordsArray.Length-2; i++)
  16.         {
  17.             if (wordsArray[i].Contains("folder name=") && wordsArray[i+1].StartsWith($"{startingLetter}"))
  18.             {
  19.                 result.Add(wordsArray[i + 1]);
  20.             }
  21.         }
  22.  
  23.      
  24.         return result;
  25.     }
  26.  
  27.     public static void Main(string[] args)
  28.     {
  29.         string xml =
  30.             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
  31.             "<folder name=\"c\">" +
  32.                 "<folder name=\"program files\">" +
  33.                     "<folder name=\"uninstall information\" />" +
  34.                 "</folder>" +
  35.                 "<folder name=\"users\" />" +
  36.             "</folder>";
  37.  
  38.         foreach (string name in Folders.FolderNames(xml, 'u'))
  39.             Console.WriteLine(name);
  40.  
  41.         Console.ReadLine();
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement