-1

我有以下代码:

var sd = new ServerDocument(documentPath);

应该注意的是 documentPath 是一个有效的路径,并且该文档使用了 vsto 应用程序。它可以很好地用 word 打开,我可以毫无问题地使用我的 vsto 扩展。

提到的代码行引发异常:

缺少根元素。

在 System.Xml.XmlTextReaderImpl.Throw(Exception e) 在 System.Xml.XmlTextReaderImpl.ParseDocumentContent() 在 System.Xml.XmlTextReaderImpl.Read() 在 System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)在 System.Xml.XmlDocument.Load(XmlReader reader) 在 System.Xml.XmlDocument.LoadXml(String xml) 在 Microsoft.Office.Tools.OfficeOpenXmlAppInfoDocument.ServerAppInfo.TryGetManifestInfo(PackagePart part, ManifestType& type, String& xml) 在 Microsoft.Office .Tools.OfficeOpenXmlAppInfoDocument.ServerAppInfo.EnsureMaps() 在 Microsoft.Office.Tools.OfficeOpenXmlAppInfoDocument.ServerAppInfo..ctor(Package package) 在 Microsoft.Office.Tools.OfficeOpenXmlAppInfoDocument.EnsureAppInfo()
在 Microsoft.Office.Tools.OfficeOpenXmlAppInfoDocument.Microsoft.VisualStudio.Tools.Applications.Runtime.IAppInfo.ReadItem(String type, String id) 在 Microsoft.VisualStudio.Tools.Applications.AppInfo.GetDeploymentManifestUrl(IAppInfo appInfo) 在 Microsoft.VisualStudio。 Tools.Applications.ServerDocument.Initialize(Byte[] bytes, String documentPathOrFileType, FileAccess access, Boolean throwOnPreviousVersion) 在 Microsoft.VisualStudio.Tools.Applications.ServerDocument.Initialize(Byte[] bytes, String documentPathOrFileType, FileAccess access) 在 Microsoft.VisualStudio .Tools.Applications.ServerDocument..ctor(String documentPath) at ConsoleApplication12.Program.Main(String[] args)

如何预防这个问题?

4

1 回答 1

0

你的文档有根元素吗?

你得到的错误:

“缺少根元素。”

表明您的文档缺少根元素。那么什么是“根元素”?

维基百科说:

每个 XML 文档都只有一个根元素。它包含所有其他元素,因此是所有其他元素的唯一父元素。ROOT 元素也称为 PARENT 元素。

这是一个例子:

<animals>
    <cat />
    <dog />
    <armadillo />
</animals>

animals是本例中的根元素。

简而言之 - 检查您的文档以确保它只有一个根元素。

于 2016-03-02T13:03:21.050 回答