Last active
December 17, 2015 19:18
-
-
Save Fitzse/5659026 to your computer and use it in GitHub Desktop.
LINQPad snippet to read a FreeMind mind map file (.mm) and get the nested Node hierarchy. Also writes formatted lines to file (tab indented hierarchy).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
var outputPath = "Something.txt"; | |
var inputPath = "Something.mm"; | |
var nodes = LoadFromFile(inputPath); | |
var lines = nodes.SelectMany(x => GetPrefixedString(x,"")); | |
lines.Dump(); | |
File.WriteAllLines(outputPath,lines); | |
} | |
static IEnumerable<string> GetPrefixedString(Node node, string prefix){ | |
prefix += "\t"; | |
var nodeString = prefix + node.Text; | |
return nodeString | |
.Unit() | |
.Concat(node.Children.SelectMany(x => GetPrefixedString(x,prefix))); | |
} | |
static IEnumerable<Node> LoadFromFile(string filePath){ | |
var xdoc = XDocument.Load(filePath); | |
var root = xdoc.Root; | |
if(root != null){ | |
return root.Elements("node").Select(CreateNode); | |
} | |
return Enumerable.Empty<Node>(); | |
} | |
static Node CreateNode(XElement element){ | |
var text = ""; | |
var textAttribute = element.Attribute("TEXT"); | |
if(textAttribute != null){ | |
text = textAttribute.Value; | |
} | |
return new Node(text){ Children = element.Elements("node").Select(CreateNode) }; | |
} | |
class Node{ | |
public string Text{get;set;} | |
public IEnumerable<Node> Children {get;set;} | |
public Node(string text){ | |
Text = text; | |
Children = Enumerable.Empty<Node>(); | |
} | |
} | |
public static class MyExtensions | |
{ | |
public static IEnumerable<T> Unit<T>(this T item){ | |
yield return item; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment