OneNote API中的XML错误无效

时间:2014-10-09 11:53:02

标签: xml api onenote

我正在尝试通过COM API在OneNote的新页面中创建一个表。它回来时带有无效的XML,但我无法弄清楚为什么......我很感激任何专家帮助:)

我收到以下错误:

来自HRESULT的异常:0x80042001

运行此代码时:

    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;
    }

错误lokup返回:

hrInvalidXML
0x80042001
The XML is invalid.

以下两个XML示例都失败了......

以下是我正在加载的一些XML:

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

这是主要的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>

1 个答案:

答案 0 :(得分:2)

您的XML存在一些问题:

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

  2. 您的Column元素缺少必需的indexwidth属性,这使您的XML无效(不符合XSD架构)。对于创建表,如果您不想指定列宽,则无需提供ColumnsColumn元素。一旦页面更新,它们就会被创建。


  3. 按原创海报编辑

    如果有人感兴趣\有类似问题我改变了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);