“该部分是只读的".使用其API向OneNote添加内容时 [英] "The section is read-only" when adding content to OneNote using its API

查看:151
本文介绍了“该部分是只读的".使用其API向OneNote添加内容时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在尝试使用Microsoft.Office.Interop.OneNote参考提供的OneNote API中的UpdatePageContent函数将内容添加到OneNote 2013页面.但是,我总是获得错误代码0x80042001,该错误代码在文​​档中被描述为 XML无效.但是,我无法理解以下简单代码中的XML无效的原因是什么.

Currently I am trying to add content to a OneNote 2013 page using the UpdatePageContent function from the OneNote API provided by the Microsoft.Office.Interop.OneNote reference. However, I always obtain the error code 0x80042001, which is described as The XML is invalid in the documentation. Yet, I cannot understand for what reason my XML in the following simple code would be invalid.

using System;
using OneNote = Microsoft.Office.Interop.OneNote;

namespace ConsoleApplicationOneNoteTest
{
    class Program
    {
        static void Main(string[] args)
        {
            OneNote.Application onenoteApp = new OneNote.Application();

            string pageChangesXml = "<?xml version=\"1.0\"?>" +
                "<one:Page xmlns:one=\"http://schemas.microsoft.com/office/onenote/2013/onenote\" " +
                "ID=\"{787BF30C-0B35-4D74-A13B-AD00D046F97D}{1}{E19479572117852244555720107518407865185197031}\">" +
                "<one:Outline>" +
                "<one:OEChildren>" +
                "<one:OE>" +
                "<one:T>" +
                "<![CDATA[New Text]]>" +
                "</one:T>" +
                "</one:OE>" +
                "</one:OEChildren>" +
                "</one:Outline>" +
                "</one:Page>";
           onenoteApp.UpdatePageContent(pageChangesXml, DateTime.MinValue);
        }
    }
}

页面ID是从OneNote Notebook层次结构中获得的,我可以确认它是正确的,因为使用相同的ID调用GetPageContent可以正常工作.因此,在我看来,XML实际上必须存在一些问题.我错过了什么?

The page ID was obtained from the OneNote Notebook hierarchy and I can confirm it to be correct, since calls to GetPageContent using the same ID work fine. So it appears to me that there actually must exist some problem with the XML. What am I missing out?

根据dbc的评论,我更深入地研究了答案,该答案最初似乎来自

Following dbc's comment, I looked deeper into this answer, which originally appears to come from here, although it treats editing an existing entry in a OneNote page instead of adding new content, which is what I want to do. Reducing the suggested code to the necessary leaves me with the following:

using System;
using System.Linq;
using XML = System.Xml.Linq;
using OneNote = Microsoft.Office.Interop.OneNote;

class Program
{
    static void Main(string[] args)
    {
        OneNote.Application onenoteApp = new OneNote.Application();
        XML.XNamespace ns = GetNamespace(onenoteApp);
        string PageId = "{787BF30C-0B35-4D74-A13B-AD00D046F97D}{1}{E19479572117852244555720107518407865185197031}";
        string xml;

        onenoteApp.GetPageContent(PageId, out xml, OneNote.PageInfo.piAll);

        var doc = XML.XDocument.Parse(xml);
        var outLine = doc.Descendants(ns + "Outline").First();
        var content = outLine.Descendants(ns + "T").First();
        string contentVal = content.Value;
        content.Value = "modified";

        onenoteApp.UpdatePageContent(doc.ToString(), DateTime.MinValue);
    }

    static XML.XNamespace GetNamespace(OneNote.Application app)
    {
        string xml;

        app.GetHierarchy(null, OneNote.HierarchyScope.hsNotebooks, out xml);
        var doc = XML.XDocument.Parse(xml);
        return doc.Root.Name.Namespace;
    }
}

但是,我现在收到错误0x8004200B,在文档中将其解释为该部分为只读.当然,从未设置过只读访问权限,并且手动编辑该页面没有麻烦.另外,对于该错误,Google并没有真正的帮助.

However, now I am recieving error 0x8004200B, which is explained as The section is read-only in the documentation. Of course no read-only access was ever set and manually I have no trouble editing said page. Also, Google is not really helpful on that error.

也许不同的错误代码意味着我的XML现在具有一种格式,该格式已被OneNote接受(尽管我看不出与以前的方法有所不同).但是我仍然没有运气来修改现有页面.特别是,两个链接的线程建议上面的代码应该开箱即用,因为在两种情况下它都是公认的答案.另一方面,在我的情况下,这根本不起作用.这是怎么回事?

Maybe the different error code means that my XML now has a format, which is accepted by OneNote (although I cannot see a difference to my previous approach). But still I have no luck in modifying an existing page. In particular, the two linked threads suggest that above code should work out-of-the-box, since it is the accepted answer in both cases. In my scenario, on the other hand, this doesn't work at all. What is going on here?

我的第二个代码块在运行Win 7 x64,OneNote 2013和Visual Studio 2015的另一台机器上工作,到目前为止,我在那台机器上运行Win 7 x86,OneNote 2013和Visual Studio 2010. VS2010库只是为了与OneNote 2013正确对话而已?我将尝试收集更多信息,看看是否可以从中得出答案.

Edit 2: My second code block works on a different machine running Win 7 x64, OneNote 2013 and Visual Studio 2015, where the machine I used so far is running Win 7 x86, OneNote 2013 and Visual Studio 2010. So maybe the VS2010 library is just to old to talk to OneNote 2013 properly? I will try to collect some more information and see if I can make an answer out of it.

推荐答案

我在使用C#和OneNote时也遇到了问题.每次我调用写入"方法时,这意味着我想编辑OneNote文件中的内容,就会遇到HRESULT异常:0x8004200B.

I've had also problems working with C# and OneNote. Everytime I was calling a "write"-method, meaning that I wanted to edit something in a OneNote-File, I got the Exception of HRESULT: 0x8004200B.

我使用Microsoft OneNote 12.0类型的库作为参考.我也尝试使用14.0类型的Library,但是在每种方法上,编译器都说类似

I was using the Microsoft OneNote 12.0 type Library as Reference. I tried to use the 14.0 type Library as well, but on every method the complier said something like

Error 1 'Microsoft.Office.Interop.OneNote.Application' does not contain a definition for 'GetHierarchy' and no extension method 'GetHierarchy' accepting a first argument of type ...

所以对我来说,解决方案是使用OneNote 14.0类型库,并将嵌入互操作类型"设置为FALSE.您可以通过单击探针中的OneNote参考(如果已添加)来找到它.

So the solution for me was using the OneNote 14.0 type Library AND set the Embed Interop Types to FALSE. You can find it by clicking on the OneNote Reference (when you already added it) down in the proberties.

从那时起,一切正常!

这篇关于“该部分是只读的".使用其API向OneNote添加内容时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆