Skip to content

Instantly share code, notes, and snippets.

@Fitzse
Last active December 17, 2015 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fitzse/5659026 to your computer and use it in GitHub Desktop.
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).
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