Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Collections;
- using System.Text;
- using System.IO;
- namespace HTMLTreeTest
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- comboBox1.SelectedIndex = 0;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- exportFileDialog.FileName = "";
- exportFileDialog.FilterIndex = 1;
- if (exportFileDialog.ShowDialog() != DialogResult.Cancel)
- {
- System.IO.StreamWriter strWri = new System.IO.StreamWriter(exportFileDialog.FileName);
- buildHTMLTree(strWri);
- strWri.Close();
- MessageBox.Show(
- "Saved to file " + exportFileDialog.FileName + "!",
- "",
- MessageBoxButtons.OK,
- MessageBoxIcon.Information);
- }
- }
- private ArrayList simulateInput()
- {
- ArrayList rows = new ArrayList();
- for (int i = 1; i <= 9; i++)
- {
- if (i == 1)
- rows.Add(new KeywordRows("keyword_" + i.ToString(), "keyword_" + i.ToString()));
- for (int x = 1; x <= (Convert.ToInt32(comboBox1.Text) / 9); x++)
- {
- if (i == 1)
- rows.Add(new KeywordRows("keyword_" + i.ToString() + "-" + x.ToString(), "keyword_" + i.ToString()));
- else
- rows.Add(new KeywordRows("keyword_" + i.ToString() + "-" + x.ToString(), "keyword_" + (i - 1).ToString() + "-" + x.ToString()));
- }
- }
- return rows;
- }
- private void buildHTMLTree(StreamWriter strWri)
- {
- string template = Properties.Resources.htmlTree_tmpl;
- string[] templateParts = template.Split(new[] { "{KEYWORDS}" }, StringSplitOptions.None);
- Dictionary<string, string> parents = new Dictionary<string, string>();
- Dictionary<string, string> childs = new Dictionary<string, string>();
- ArrayList array = new ArrayList();
- array = simulateInput();
- foreach (KeywordRows kwd in array)
- {
- if (kwd.root_keyword == kwd.keyword)
- {
- if (!parents.ContainsKey(kwd.keyword))
- parents.Add(kwd.keyword, kwd.root_keyword);
- }
- else
- {
- if (!childs.ContainsKey(kwd.keyword))
- childs.Add(kwd.keyword, kwd.root_keyword);
- }
- }
- strWri.Write(templateParts[0]);
- strWri.Write("<ul id=\"parents\">");
- foreach (string parent in parents.Values)
- {
- strWri.Write("<li id=\"{0}\">{0}", parent);
- if (childs.ContainsValue(parent))
- {
- strWri.Write("<ul id=\"parents\">");
- process(ref childs,strWri, parent);
- strWri.Write("</ul>");
- }
- strWri.Write("</li>");
- }
- strWri.Write("</ul>");
- strWri.Write(templateParts[1]);
- //return Properties.Resources.htmlTree_tmpl.Replace("{KEYWORDS}", sb.ToString());
- }
- public void process(ref Dictionary<string, string> _childs, StreamWriter strWri, string parent)
- {
- var getChilds =
- from o in _childs
- where (o.Value == parent)
- select o.Key;
- foreach (var tmp in getChilds)
- {
- string child = tmp.ToString();
- if (_childs.ContainsValue(child))
- {
- strWri.Write("<li id=\"{0}\">{0}<ul id=\"{0}\">", child);
- process(ref _childs, strWri, child);
- strWri.Write("</ul></li>");
- }
- else
- {
- strWri.Write("<li id=\"{0}\">{0}</li>", child);
- }
- }
- return;
- }
- public class KeywordRows
- {
- private string _keyword;
- private string _root_keyword;
- public KeywordRows(string keyword, string root_keyword)
- {
- _keyword = keyword;
- _root_keyword = root_keyword;
- }
- public string keyword
- {
- get
- {
- return _keyword;
- }
- set
- {
- _keyword = value;
- }
- }
- public string root_keyword
- {
- get
- {
- return _root_keyword;
- }
- set
- {
- _root_keyword = value;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement