Skip to content

Instantly share code, notes, and snippets.

@atcarter714
Created March 19, 2023 23:15
Show Gist options
  • Save atcarter714/7ad976d2351596687e3e6bdf42d97609 to your computer and use it in GitHub Desktop.
Save atcarter714/7ad976d2351596687e3e6bdf42d97609 to your computer and use it in GitHub Desktop.
Read a OneNote hierarchy in C# ....
static void Main(string[] args) {
// OneNote 2013 Schema namespace.
string strNamespace = "http://schemas.microsoft.com/office/onenote/2013/onenote";
string outputXML;
Application onApplication = new Application();
onApplication.GetHierarchy(null, HierarchyScope.hsSections, out outputXML);
// Load a new XmlDocument.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(outputXML);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("one", strNamespace);
// Search for the section named "Sample_Section".
XmlNode xmlNode = xmlDoc.SelectSingleNode("//one:Section[@name='Sample_Section']", nsmgr);
// Prompt for a new section title.
System.Console.Write("Please enter a new title for the section: ");
string input = System.Console.ReadLine();
xmlNode.Attributes["name"].Value = input;
// Update the section with the new title.
onApp.UpdateHierarchy(xmlNode.OuterXml);
System.Console.Write("Done!\n");
}
@atcarter714
Copy link
Author

Just a handy snippet I wanted to save and import programmatically for examples ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment