January 22, 2007

Sample Code for traversing a MindManager xml file

MindJet's MindManager is a good mind mapping software. The best part is it stores the content in an xml file. Locate your mindmanager file, change the extension to .zip and extract the contents.
What you will see is a xml file and associated schemas and image files.

I have written some sample code traversing this file and adding the structure to a treeview. Here is the code sample.

private void btnLoad_Click(object sender, EventArgs e)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(@"..\..\SampleData\Document.xml");

TreeNode trn = new TreeNode("Root");
trv.Nodes.Add(trn);

// Create an XmlNamespaceManager to resolve the default namespace.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
nsmgr.AddNamespace("mm", "http://schemas.mindjet.com/MindManager/Application/2003");

string xpathQuery = "/mm:Map/mm:OneTopic/mm:Topic";

RecurseChilds(
xdoc.ChildNodes.Item(1).SelectNodes(xpathQuery, nsmgr),
trn,
nsmgr);


}

public void RecurseChilds(XmlNodeList xnl, TreeNode root, XmlNamespaceManager nsmgr)
{
if (xnl != null && xnl.Count > 0)
{
foreach (XmlNode xn in xnl)
{
XmlNode xnTitle= xn.SelectSingleNode("./mm:Text", nsmgr);
if (xnTitle != null)
{
XmlNode xnHyperlink = xn.SelectSingleNode("./mm:Hyperlink", nsmgr);
string treeText = xnTitle.Attributes["PlainText"].Value;
TreeNode trn = new TreeNode(treeText);
if (xnHyperlink != null)
trn.ToolTipText = xnHyperlink.Attributes["Url"].Value;
root.Nodes.Add(trn);
XmlNodeList xnlChildNodes = xn.SelectNodes("./mm:SubTopics/mm:Topic", nsmgr);
RecurseChilds(
xnlChildNodes,
trn,
nsmgr);
}

}
}
}

No comments:

Post a Comment