1

I am trying to create a table in a new page on OneNote via the COM API. It's coming back with invalid XML but I can't figure out why... I'd appreciate any expert help :)

I'm getting the following error:

Exception from HRESULT: 0x80042001

When running this code:

    static string CreatePage(string sectionId, string pageName)
    {
        // Create the new page
        string pageId;
        onenoteApp.CreateNewPage(sectionId, out pageId, NewPageStyle.npsBlankPageWithTitle);

        var doc = CreateTable();

        // Update the page
        onenoteApp.UpdatePageContent(doc.ToString());

        return pageId;
    }

The error lokup comes back with:

hrInvalidXML
0x80042001
The XML is invalid.

Both the XML examples below fail...

Here's some cut down XML I'm loading:

<Page>  <Outline>    <OEChildren>      <OE>                  <T><![CDATA[test 2-3 ]]></T>      </OE>    </OEChildren>  </Outline></Page>

Here's the main XML

<Page xmlns="http://schemas.microsoft.com/office/onenote/2010/onenote">
  <Outline>
    <OEChildren>
      <OE>
        <Table>
          <Columns>
            <Column />
            <Column />
            <Column />
          </Columns>
          <Row>
            <Cell>
              <OEChildren>
                <OE>
                  <T><![CDATA[test 1-1 ]]></T>
                </OE>
              </OEChildren>
            </Cell>
            <Cell>
              <OEChildren>
                <OE>
                  <T><![CDATA[test 1-2 ]]></T>
                </OE>
              </OEChildren>
            </Cell>
            <Cell>
              <OEChildren>
                <OE>
                  <T><![CDATA[test 1-3 ]]></T>
                </OE>
              </OEChildren>
            </Cell>
          </Row>
          <Row>
            <Cell>
              <OEChildren>
                <OE>
                  <T><![CDATA[test 2-1 ]]></T>
                </OE>
              </OEChildren>
            </Cell>
            <Cell>
              <OEChildren>
                <OE>
                  <T><![CDATA[test 2-2 ]]></T>
                </OE>
              </OEChildren>
            </Cell>
            <Cell>
              <OEChildren>
                <OE>
                  <T><![CDATA[test 2-3 ]]></T>
                </OE>
              </OEChildren>
            </Cell>
          </Row>
        </Table>
      </OE>
    </OEChildren>
  </Outline>
</Page>
4

1 回答 1

3

您的 XML 存在一些问题:

  1. 您的Page元素没有该ID属性。没有它,OneNote 不知道您要更新哪个页面。一般来说,您应该先阅读页面并修改 XML,然后使用新的 XML 更新它。这将保留您不打算修改的任何内容。

  2. 您的Column元素缺少必需的属性indexwidth属性,这使您的 XML 无效(不符合 XSD 架构)。对于创建表格,如果您不想指定列宽,您甚至不需要提供ColumnsColumn元素。它们将在页面更新后创建。


由原始海报编辑

如果有人感兴趣\有类似的问题,我会更改 CreateTable 函数

static XDocument CreateTable(string PageID)
{
    var page = new XDocument(new XElement(ns + "Page",
       new XElement(ns + "Outline",
         new XElement(ns + "OEChildren",
           new XElement(ns + "OE",
             new XElement(ns + "Table",
                new XElement(ns + "Row",
                    new XElement(ns + "Cell",
                         new XElement(ns + "OEChildren",
                           new XElement(ns + "OE",
                             new XElement(ns + "T",
                               new XCData("test 1-1 "))))),
                    new XElement(ns + "Cell",
                         new XElement(ns + "OEChildren",
                           new XElement(ns + "OE",
                             new XElement(ns + "T",
                               new XCData("test 1-2 "))))),
                    new XElement(ns + "Cell",
                         new XElement(ns + "OEChildren",
                           new XElement(ns + "OE",
                             new XElement(ns + "T",
                               new XCData("test 1-3 ")))))),
                new XElement(ns + "Row",
                    new XElement(ns + "Cell",
                         new XElement(ns + "OEChildren",
                           new XElement(ns + "OE",
                             new XElement(ns + "T",
                               new XCData("test 2-1 "))))),
                    new XElement(ns + "Cell",
                         new XElement(ns + "OEChildren",
                           new XElement(ns + "OE",
                             new XElement(ns + "T",
                               new XCData("test 2-2 "))))),
                    new XElement(ns + "Cell",
                         new XElement(ns + "OEChildren",
                           new XElement(ns + "OE",
                             new XElement(ns + "T",
                               new XCData("test 2-3 "))))))))))));

    page.Root.SetAttributeValue("ID", PageID);

    return page;
}

并用这个来称呼它:

var doc = CreateTable(pageId);
于 2014-10-10T03:22:10.887 回答