Table trouble

 

I had to relearn the lesson of paying attention to details recently.  I was writing some code to work with OneNote's table XML via extensibility, and could not get my code to work correctly.  After a couple of days of working on this in my spare time, I was just about ready to give up.  I wound up in the situation in which I could add a row, but OneNote would only recognize a single cell from that row.  In other words, the number of rows would increase by 1 as desired, but the number of cells would only increase by 1 as well, instead of increasing by the number of columns.

 

I was using this code to create a new row node to add to an existing table:

 

XmlNode newChildRow;

newChildRow = pageDoc.CreateElement("one:Row", strNamespace);

for (int i = 0; i < numColumns; i++)

{

XmlNode newCell = pageDoc.CreateElement("one:Cell");

newChildRow.AppendChild(newCell);

XmlNode newOEChildren = pageDoc.CreateElement("one:OEChildren");

newChildRow.AppendChild(newOEChildren);

XmlNode newOE = pageDoc.CreateElement("one:OE");

newChildRow.AppendChild(newOE);

XmlNode newT = pageDoc.CreateElement("one:T");

newChildRow.AppendChild(newT);

}

 

When I would look at the page XML after adding the node, this is what the final node looked like:

<one:Row>

<one:Cell />

<one:OEChildren />

<one:OE />

<one:T />

<one:Cell />

<one:OEChildren />

<one:OE />

<one:T />

<one:Cell />

<one:OEChildren />

<one:OE />

<one:T />

</one:Row>

 

 

And that is valid XML. 

 

 

But bombs in OneNote.  I would query the table for the number of rows after my code would run, and it would be what I expected.  But the number of data cells would only go up by 1.  I thought this was timing related or related to the cache being up to date but not the file and spent a lot of time working on that faulty supposition. 

 

Then I started looking very closely at the XML.  Check it out closely: the first cell is a child to the row, but the first OEChildren is a sibling to the Cell, where it really should be a child.  Sigh.

 

Here’s the correct code:

 

            XmlNode newChildRow;

            newChildRow = pageDoc.CreateElement("one:Row", strNamespace);

            for (int i = 0; i < numColumns; i++)

            {

                XmlNode newCell = pageDoc.CreateElement("one:Cell",strNamespace );

                newChildRow.AppendChild(newCell);

                XmlNode newOEChildren = pageDoc.CreateElement("one:OEChildren", strNamespace);

                newCell.AppendChild(newOEChildren);

                XmlNode newOE = pageDoc.CreateElement("one:OE", strNamespace);

                newOEChildren.AppendChild(newOE);

                XmlNode newT = pageDoc.CreateElement("one:T", strNamespace);

                newOE.AppendChild(newT);

            }

 

Notice how the children are correctly appended to the correct parent.

 

And the final XML:

<one:Row>

<one:Cell>

<one:OEChildren>

<one:OE>

                            <one:T />

               </one:OE>

      </one:OEChildren>

  </one:Cell>

<one:Cell>

<one:OEChildren>

<one:OE>

                            <one:T />

               </one:OE>

      </one:OEChildren>

 </one:Cell>

</one:Row>

 

 Sigh.  This all boiled down to a simple child/parent problem with XML. 

 

Long time readers will wonder why I was working with table XML - let me just drop a hint to expect some useful output from this exercise soon…

 

Questions, comments, concerns and criticisms always welcome,

John