NullReferenceException XAttribute Value

时间:2017-07-28 15:55:00

标签: c#

大家好我正在开发一个Word加入,一切正常,除非我运行调试器时它说明了这一行:

        return node.Attribute("ID").Value;

抛出NullReferenceException,这是整个程序上下文中的代码:

using System;
using System.Linq;
using System.Xml.Linq;
using OneNote = Microsoft.Office.Interop.OneNote;
using Microsoft.Office.Interop.OneNote;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Word;

namespace WordAddIn1
{
    public partial class ThisAddIn
    {

        //creates a new OneNote Application object
        static OneNote.Application onenoteApp;
        static XNamespace ns = null;
        static string resultWordText = "";

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            onenoteApp = new OneNote.Application();
            getWordText();
            writeToOneNote();
        }

        public static void getWordText()
        {
            try
            {
                Microsoft.Office.Interop.Word.Application msWordApp = new Microsoft.Office.Interop.Word.Application();
                object nullObj = System.Reflection.Missing.Value;
                object oFalse = false;
                object oFile = "C:\\Users\\t-aaalle\\Desktop\\GreetingFile.docx";

                Microsoft.Office.Interop.Word.Document doc = msWordApp.Documents.Open(
                                                             ref oFile, ref nullObj, ref nullObj,
                                                             ref nullObj, ref nullObj, ref nullObj,
                                                             ref nullObj, ref nullObj, ref nullObj,
                                                             ref nullObj, ref nullObj, ref nullObj,
                                                             ref nullObj, ref nullObj, ref nullObj,
                                                             ref nullObj);
                resultWordText = doc.Content.Text.Trim();
                doc.Close(ref oFalse, ref nullObj, ref nullObj);
                Console.WriteLine(resultWordText);
                Console.Read();
                Process.Start("onenote.exe");

            }
            catch (Exception e)
            {
                var trace = new System.Diagnostics.StackTrace(e);
            }
        }

        public static void writeToOneNote()
        {
            GetNamespace();
            //Gets the notebook, section, and firstpage
            string notebookId = GetObjectId(null, OneNote.HierarchyScope.hsNotebooks, "Aaron @ Microsoft");
            string sectionId = GetObjectId(notebookId, OneNote.HierarchyScope.hsSections, "New Section 1");
            string firstPageId = GetObjectId(sectionId, OneNote.HierarchyScope.hsPages, "My Page");

            //Reads the contents of the first page
            GetPageContent(firstPageId);
            Console.Read();
        }


        //Gets the name of the notebook 
        static void GetNamespace()
        {
            string xml = "";

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


        //Gets the OneNote document
        static string GetObjectId(string parentId, HierarchyScope scope, string objectName)
        {
            string xml = "";

            onenoteApp.GetHierarchy(parentId, scope, out xml);

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

            switch (scope)
            {
                case (HierarchyScope.hsNotebooks): nodeName = "Notebook"; break;
                case (HierarchyScope.hsPages): nodeName = "Page"; break;
                case (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; //THIS IS THE LINE
        }

        //Get's the pages content 
        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 = resultWordText;
            onenoteApp.UpdatePageContent(doc.ToString());
            return null;
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}

当有人告诉我为什么会发生这种情况时,我在调试器中运行它会说有一个ID值被传递给Value,但是它给了我这个没有意义的错误。帮助识别问题会非常棒!

0 个答案:

没有答案