如何写入的OneNote 2013页使用C#和OneNote的互操作 [英] How To Write To A OneNote 2013 Page Using C# and The OneNote Interop

查看:412
本文介绍了如何写入的OneNote 2013页使用C#和OneNote的互操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了这个,但所有的人都很多文章要么不齐全或者不回答我的问题。使用 C#和OneNote的互操作,我想简单地写文本到现有的OneNote 2013页。目前,我有一个OneNote记事本,标题为Sample_Section 一科和一个名为我的页面页面。

I have seen many articles about this but all of them are either incomplete or do not answer my question. Using C# and the OneNote Interop, I would like to simply write text to an existing OneNote 2013 Page. Currently I have a OneNote Notebook, with a Section titled "Sample_Section" and a Page called "MyPage".

我需要能够使用 C#代码来写文本这个页面,但我无法弄清楚如何或发现任何资源这样做。我看过的所有网站上的代码示例,没有回答这个简单的问题,或者能够做到这一点。也有很多例子代码已经过时,试图运行它们时破裂。

I need to be able to use C# code to write text to this Page, but I cannot figure out how or find any resources to do so. I have looked at all of the code examples on the web and none answer this simple question or are able to do this. Also many of the code examples are outdated and break when attempting to run them.

我用微软代码示例演示如何更改段的名字,但我无法找到任何代码写文本到。有没有简单的方法来做到这一点,我可以看到。我已经采取了很多时间来研究这一点,并查看不同的例子在线,但没有人能够提供帮助。

I used the Microsoft code sample that shows how to change the name of a Section but I cannot find any code to write text to a Page. There is no simple way to do this that I can see. I have taken a lot of time to research this and view the different examples online but none are able to help.

我已经查看关于 OneNote中 MSDN 文章 互操作为好。我隐约明白是怎么的OneNote 互操作致力于通过 XML ,但任何额外的帮助,理解,也将不胜感激。最重要的是我会很感激,演示如何写文本到的OneNote 2013笔记本页面的代码示例。

I have already viewed the MSDN articles on the OneNote Interop as well. I vaguely understand how the OneNote Interop works through XML but any extra help understanding that would also be appreciated. Most importantly I would really appreciate a code example that demonstrates how to write text to a OneNote 2013 Notebook Page.


创建:我已经使用这个堆栈溢出的答案试过从C#新一注2010页

不过,也有这个解决方案没有回答我的问题两件事情:

However, there are 2 things about this solution that do not answer my question:

1)显着的解决方案,展示了如何创建一个新的页面,而不是如何写文字,或如何使用任何信息来填充页面。

1) The marked solution shows how to create a new page, not how to write text to it or how to populate the page with any information.

2)当我尝试运行标记为解决方案的代码,我得到以下行错误:

2) When I try to run the code that is marked as the solution, I get an error at the following line:

var node = doc.Descendants(ns + nodeName).Where(n => n.Attribute("name").Value == objectName).FirstOrDefault();
return node.Attribute("ID").Value;



原因是,节点的值为null,任何帮助将不胜感激。

The reason being that the value of "node" is null, any help would be greatly appreciated.

推荐答案

我问在MSDN论坛上同样的问题,并给出这个伟大的答案。我希望这可以帮助人们在未来

I asked the same question on MSDN forums and was given this great answer. I hope that this can help people in the future.

    static Application onenoteApp = new Application();
    static XNamespace ns = null;

    static void Main(string[] args)
    {
        GetNamespace();
        string notebookId = GetObjectId(null, OneNote.HierarchyScope.hsNotebooks, "MyNotebook");
        string sectionId = GetObjectId(notebookId, OneNote.HierarchyScope.hsSections, "Sample_Section");
        string firstPageId = GetObjectId(sectionId, OneNote.HierarchyScope.hsPages, "MyPage");
        GetPageContent(firstPageId);
        Console.Read();
    }
    static void GetNamespace()
    {
        string xml;

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

    static string GetObjectId(string parentId, OneNote.HierarchyScope scope, string objectName)
    {
        string xml;
        onenoteApp.GetHierarchy(parentId, scope, out xml);

        var doc = XDocument.Parse(xml);
        var nodeName = "";

        switch (scope)
        {
            case (OneNote.HierarchyScope.hsNotebooks): nodeName = "Notebook"; break;
            case (OneNote.HierarchyScope.hsPages): nodeName = "Page"; break;
            case (OneNote.HierarchyScope.hsSections): nodeName = "Section"; break;
            default:
                return null;
        }

        var node = doc.Descendants(ns + nodeName).Where(n => n.Attribute("name").Value == objectName).FirstOrDefault();

        return node.Attribute("ID").Value;
    }
    static string GetPageContent(string pageId)
    {
        string xml;
        onenoteApp.GetPageContent(pageId, out xml, OneNote.PageInfo.piAll);
        var doc = 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());
        return null;
    }

这篇关于如何写入的OneNote 2013页使用C#和OneNote的互操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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