OneNote Import Managed Assembly: The Quick Rundown (2 of 2)

In part one, we discussed how to use the OneNote managed class assembly to import content into OneNote. Now let’s actually look at the XML the Page class generates for the SimpleImporter.Import method.

Looking at the XML

If you want to preview what the XML that’s passed to the SimpleImport.Import method will look like before you run the Commit method, call the Page.ToString method. This is the method that actually serializes the page and its contents to XML prior to importing. Just remember that once you’ve committed a page, none of the object on that page will be included in the generated import XML, unless they’ve subsequently been modified in some way.

Also remember that the Commit method generates import XML with both <EnsurePage> and <PlaceObjects> elements for that page. Specifying an <EnsurePage> element for a page guarantees that the page exists before OneNote attempts to import objects onto it. So if your application includes a scenario where you only want to import objects onto a page if the page already exists, you’ll need to modify this method, or use another means.

So let’s a quick look at the code sample Donovan gives over on his blog (I think he’ll forgive me if I rewrite it in VB). This sample creates a new page, title “General”, with one object on that page: an outline containing the text “Hello world!”

Sub TrivialImportVB()

   Dim p As New Page("General")

   Dim outline As New OutlineObject

   outline.AddContent(New HtmlContent("Hello <i>world</i>!"))

   p.AddObject(outline)

   p.Commit()

   p.NavigateTo()

End Sub

And here’s the resulting XML that gets passed to the SimpleImport.Import method. Notice that the necessary GUIDs are there, and the HTML has even been cleaned up so that OneNote accepts it with no problem. How’s that for service?

<?xml version="1.0" encoding="utf-16"?>

<Import xmlns="https://schemas.microsoft.com/office/onenote/01/2004/import">

      <EnsurePage path="General" guid="{e5bf863a-7070-4058-a0f5-

343e4a45dc05}" date="2004-07-23T09:52:17.0963010-07:00" />

      <PlaceObjects pagePath="General" pageGuid="{e5bf863a-7070-4058-a0f5-

      343e4a45dc05}">

            <Object guid="{a5af80e4-8105-4109-b3ac-538851e9e3fa}">

                  <Position x="36" y="36" />

                  <Outline>

                              <Html><Data><![CDATA[<html><body>Hello

<i>world</>!</body></html>]]></Data></Html>

                  </Outline>

            </Object>

      </PlaceObjects>

</Import>

Object Model Diagrams

And what’s an object model without an object model map? Anyone who’s read this blog has probably realized how fond I am of presenting technical information graphically (which I’m sure has nothing to do with my being the programmer-writer for PowerPoint and Publisher.) So here are some diagrams of how the classes fit together. But first, some caveats:

The following diagrams show only the public members of each class. None of the abstract classes are diagramed, and I’ve left any inheritance information off the diagrams, although I’ve listed any members that a class inherits in the individual classes themselves. For example, ImageObject, InkObject, and OutlineObject all inherit the Height, Position, and Width properties from the abstract class PageObject, which is not diagrammed.

I mainly want to show how the various object relate to one another. For the most part, if a member gets or sets a data type, I haven’t bothered showing that in the diagram. In addition, for the sake of clarity, I’ve left off the following information for basic methods that most classes here have:

· The Clone method returns the type of object from which you call it.

· The Equals method takes a System.Object as a parameter.

· The GetHashCode method returns a System.Int32 suitable for use in hashing algorithms and data structures like a hash table.

Trust me, I’ve got much more detailed diagrams of the internal workings of each class tacked up around my office, and those will most likely make it into the finished article, which discusses the class source code. But these should give you a good at-a-glance idea of how things flow.

 

Figure 1. Page object (and legend)

 

Figure 2. ImageObject, InkObject, and OutlineObject

 

Figure 3. HtmlContent, ImageContent, and InkContent