My program is made to generate latex files to one main latex file in winforms. I am using add nodes button event to add all my latex chapters(parent nodes) and section(children nodes). Main point of this project is to remove unwanted chapters or sections, by this option I can generate various types of main latex files. Also I am making a save and load buttons. I am saving everything to a JSON format file and later loading it. This is because I want to have a document configuration file for every generated main file( its like previous generated file) . So why I am writing here is that, my loaded button isnt doing anything with my append line( isnt filling my main latex file). If I use add_nodes button and generate file, everything is working and main file is filled with needed information, but when I load my JSON file, it is loaded correct in win forms window, but main file isnt filled. Maybe you can tell me why? and what should I do?
Here is my code:
First of all, I am adding all my parent and children node:
private void addNodeButton_Click(object sender, EventArgs e)
{
treeView1.Nodes.Add(new TreeNode("ccc") { Tag = @"\include {ccc}" });
treeView1.Nodes[0].Add(new TreeNode("bbb") { Tag = @"\input {bbb}" });
This is how I use my append line with chapters, I am using header and
footer file and with append line help I am inserting Tags to needed
place in my main file:
private void button2_Click(object sender, EventArgs e)
{
{
var header = File.ReadAllText(@"C:\dir\header.tex");
var footer = File.ReadAllText(@"C:\dir\footer.tex");
var sb = new StringBuilder();
sb.AppendLine(header);
foreach (TreeNode node in treeView1.Nodes)
{
var tag = node.Tag as string;
sb.AppendLine(string.IsNullOrEmpty(Text) ? node.Text : tag);
}
sb.AppendLine(footer);
File.WriteAllText(@"C: \dir\final.tex", sb.ToString());
}
Later, I am saving it and loading:
private void saveButton_Click(object sender, EventArgs e)
{
this.treeView1.Save(jsonFilePath);
}
private void loadButton_Click(object sender, EventArgs e)
{
this.treeView1.Nodes.Clear();
this.treeView1.Load(jsonFilePath);
}
And this is my class for loading:
public static class TreeRepository
{
public static List<DataNode> LoadDataNodes(string jsonFilePath)
{
var json = System.IO.File.ReadAllText(jsonFilePath);
var dataNodes = JsonConvert.DeserializeObject<List<DataNode>>(json);
return dataNodes;
}
public static void Load(this TreeView treeView, string jsonFilePath)
{
treeView.Nodes.Load(LoadDataNodes(jsonFilePath));
}
public static void Load(this TreeNodeCollection nodes, List<DataNode> dataNodes)
{
foreach (var dataNode in dataNodes)
{
var treeNode = nodes.Add(dataNode.Text);
treeNode.Checked = dataNode.IsChecked;
if (dataNode.Children != null && dataNode.Children.Count > 0)
{
Load(treeNode.Nodes, dataNode.Children);
}
}
}
public static List<DataNode> GetDataNodes(this TreeView treeView)
{
var dataNodes = new List<DataNode>();
AddNodesToList(treeView.Nodes, dataNodes);
return dataNodes;
}
private static void AddNodesToList(TreeNodeCollection nodes, List<DataNode> dataNodes)
{
foreach (TreeNode node in nodes)
{
var dataNode = new DataNode
{
Children = new List<DataNode>(),
IsChecked = node.Checked,
Text = node.Text
};
dataNodes.Add(dataNode);
if (node.Nodes.Count > 0)
{
AddNodesToList(node.Nodes, dataNode.Children);
}
}
}