Invalid XML error from OneNote API

寵の児 提交于 2020-01-17 03:37:05

问题


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>

回答1:


Your XML has a few problems:

  1. Your Page element doesn't have the ID attribute. Without it OneNote doesn't know which page you are trying to update. In general you should read the page first and modify the XML and then update it with the new XML. This will preserve anything you didn't intend to modify.

  2. Your Column elements are missing the required index and width attributes, which is what makes your XML invalid (does not conform to the XSD schema). For creating tables, if you don't want to specify the column widths, you don't need to even provide Columns and Column elements. They will be created once the page is updated.


Edit by Original Poster

In case anyone is interested\has similar problems I changes the CreateTable function

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

and called it with this:

var doc = CreateTable(pageId);


来源:https://stackoverflow.com/questions/26277771/invalid-xml-error-from-onenote-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!