问题
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);
}
}
}
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?
Edit: 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;
}
}
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.
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?
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.
回答1:
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.
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 ...
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.
Since then, everything worked fine!
来源:https://stackoverflow.com/questions/34849720/the-section-is-read-only-when-adding-content-to-onenote-using-its-api