Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2020
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.57 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace _2
  5. {
  6.     public class TagList
  7.     {
  8.         public String list;
  9.         public String filePath;
  10.  
  11.         public TagList(String path = null) { filePath = path; }
  12.  
  13.         public String Open(String tagName, int level, String value = null)
  14.         {
  15.             String shift = new String('\t', level);
  16.             list += String.Format("\n{0}<{1}>", shift, tagName);
  17.             if (!(value is null))
  18.                 Value(value, level);
  19.             return list;
  20.         }
  21.         public String Close(String tagName, int level)
  22.         {
  23.             String shift = new String('\t', level);
  24.             list += String.Format("\n{0}</{1}>", shift, tagName);
  25.             return list;
  26.         }
  27.  
  28.         public String Insert(String tagName, int level, String value, bool newLine = false)
  29.         {
  30.             String shift = new String('\t', level);
  31.             String enter = newLine ? "\n" : "";
  32.             list += String.Format("{0}{1}<{2}>{3}</{2}>{0}", enter, shift, tagName, value);
  33.             return list;
  34.         }
  35.  
  36.         public String Value(String value, int level = 0)
  37.         {
  38.             String shift = new String('\t', level);
  39.             list += '\n' + shift + value;
  40.             return list;
  41.         }
  42.  
  43.         public void Write(String path = null)
  44.         {
  45.             if (path is null)
  46.                 path = this.filePath;
  47.             using (FileStream fs = File.Open(path, FileMode.OpenOrCreate, FileAccess.Write))
  48.             {
  49.                 byte[] bytes = System.Text.Encoding.Default.GetBytes(list);
  50.                 fs.Write(bytes, 0, bytes.Length);
  51.             }
  52.         }
  53.  
  54.         public String Show() => list;
  55.         public void Clear() { list = new String(""); }
  56.         public String NewLine(int count = 0) => list += new String('\n', count);
  57.  
  58.     }
  59.  
  60.     class Program
  61.     {
  62.         static void Main(string[] args)
  63.         {
  64.             Console.WriteLine(CreateTagFile());
  65.         }
  66.  
  67.         static public String CreateTagFile()
  68.         {
  69.             Console.WriteLine("CreateTagFile");
  70.             Console.WriteLine(new String('-', 20));
  71.             TagList tagList = new TagList();
  72.             for (int i = 0; i < 10; i++)
  73.             {
  74.                 tagList.Open($"tag_{i + 1}", i);
  75.                 tagList.Value(Convert.ToString(i + 1), i + 1);
  76.             }
  77.             for (int i = 9; i >= 0; i--)
  78.             {
  79.                 tagList.Close($"tag_{i + 1}", i);
  80.             }
  81.             tagList.Write("file.txt");
  82.             return tagList.Show();
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement