Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- namespace _2
- {
- public class TagList
- {
- public String list;
- public String filePath;
- public TagList(String path = null) { filePath = path; }
- public String Open(String tagName, int level, String value = null)
- {
- String shift = new String('\t', level);
- list += String.Format("\n{0}<{1}>", shift, tagName);
- if (!(value is null))
- Value(value, level);
- return list;
- }
- public String Close(String tagName, int level)
- {
- String shift = new String('\t', level);
- list += String.Format("\n{0}</{1}>", shift, tagName);
- return list;
- }
- public String Insert(String tagName, int level, String value, bool newLine = false)
- {
- String shift = new String('\t', level);
- String enter = newLine ? "\n" : "";
- list += String.Format("{0}{1}<{2}>{3}</{2}>{0}", enter, shift, tagName, value);
- return list;
- }
- public String Value(String value, int level = 0)
- {
- String shift = new String('\t', level);
- list += '\n' + shift + value;
- return list;
- }
- public void Write(String path = null)
- {
- if (path is null)
- path = this.filePath;
- using (FileStream fs = File.Open(path, FileMode.OpenOrCreate, FileAccess.Write))
- {
- byte[] bytes = System.Text.Encoding.Default.GetBytes(list);
- fs.Write(bytes, 0, bytes.Length);
- }
- }
- public String Show() => list;
- public void Clear() { list = new String(""); }
- public String NewLine(int count = 0) => list += new String('\n', count);
- }
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine(CreateTagFile());
- }
- static public String CreateTagFile()
- {
- Console.WriteLine("CreateTagFile");
- Console.WriteLine(new String('-', 20));
- TagList tagList = new TagList();
- for (int i = 0; i < 10; i++)
- {
- tagList.Open($"tag_{i + 1}", i);
- tagList.Value(Convert.ToString(i + 1), i + 1);
- }
- for (int i = 9; i >= 0; i--)
- {
- tagList.Close($"tag_{i + 1}", i);
- }
- tagList.Write("file.txt");
- return tagList.Show();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement