我创建了一个带有功能区的Word插件项目,在使用OpenXml进行几次修改后,当我想要保存文档时,会引发异常。
当文档大小超过1MB时,会出现这个问题。经过多次搜索,发现“保存”操作是使用独立存储实现的解决方案如下:
并且
但是这段代码并没有解决我的问题。
Dim MainXMLDoc As New XmlDocument()
Using WordDoc As WordprocessingDocument = WordprocessingDocument.Open(DocPath, True)
Dim mainPart As MainDocumentPart = WordDoc.MainDocumentPart
If Not mainPart Is Nothing Then
MainXMLDoc.Load(mainPart.GetStream())
EXmlDocument.XMLDoc = Nothing
EXmlDocument.XMLDoc = MainXMLDoc
EXmlDocument.GetWordDocIds()
'..............
end if
'........
Dim stream As IO.Stream
stream = mainPart.GetStream(FileMode.Create, FileAccess.Write)
MainXMLDoc.Save(stream) '-----> exception
异常信息如下:
Interception de System.IO.IsolatedStorage.IsolatedStorageException
Message=Unable to determine the identity of domain. Source=mscorlib
StackTrace:
at System.IO.IsolatedStorage.IsolatedStorage._GetAccountingInfo(Evidence
evidence, Type evidenceType, IsolatedStorageScope fAssmDomApp, Object&
oNormalized)
at System.IO.IsolatedStorage.IsolatedStorage.GetAccountingInfo(Evidence
evidence, Type evidenceType, IsolatedStorageScope fAssmDomApp, String&
typeName, String& instanceName)
at System.IO.IsolatedStorage.IsolatedStorage._InitStore(IsolatedStorageScope
scope, Evidence domainEv, Type domainEvidenceType, Evidence assemEv,
Type assemblyEvidenceType, Evidence appEv, Type appEvidenceType)
at System.IO.IsolatedStorage.IsolatedStorage.InitStore(IsolatedStorageScope
scope, Type domainEvidenceType, Type assemblyEvidenceType)
at System.IO.IsolatedStorage.IsolatedStorageFile.GetStore(IsolatedStorageScope
scope, Type domainEvidenceType, Type assemblyEvidenceType)
at MS.Internal.IO.Packaging.PackagingUtilities.ReliableIsolatedStorageFileFolder.GetCurrentStore()
at MS.Internal.IO.Packaging.PackagingUtilities.ReliableIsolatedStorageFileFolder..ctor()
at MS.Internal.IO.Packaging.PackagingUtilities.GetDefaultIsolatedStorageFile()
at MS.Internal.IO.Packaging.PackagingUtilities.CreateUserScopedIsolatedStorageFileStreamWithRandomName(Int32
retryCount, String& fileName)
at MS.Internal.IO.Packaging.SparseMemoryStream.SwitchModeIfNecessary()
at MS.Internal.IO.Packaging.SparseMemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at MS.Internal.IO.Packaging.CompressEmulationStream.Write(Byte[] buffer,
Int32 offset, Int32 count)
at MS.Internal.IO.Packaging.CompressStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at MS.Internal.IO.Zip.ProgressiveCrcCalculatingStream.Write(Byte[]
buffer, Int32 offset, Int32 count)
at MS.Internal.IO.Zip.ZipIOModeEnforcingStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
at System.IO.StreamWriter.Write(Char value)
at System.Xml.XmlTextWriter.Indent(Boolean beforeEndElement)
at System.Xml.XmlTextWriter.AutoComplete(Token token)
at System.Xml.XmlTextWriter.WriteStartElement(String prefix, String localName, String ns)
at System.Xml.XmlDOMTextWriter.WriteStartElement(String prefix, String localName, String ns)
at System.Xml.XmlElement.WriteStartElement(XmlWriter w)
at System.Xml.XmlElement.WriteElementTo(XmlWriter writer, XmlElement e)
at System.Xml.XmlElement.WriteTo(XmlWriter w)
at System.Xml.XmlDocument.WriteContentTo(XmlWriter xw)
at System.Xml.XmlDocument.WriteTo(XmlWriter w)
at System.Xml.XmlDocument.Save(Stream outStream) InnerException:
当文档大小超过1MB时,会出现这个问题。经过多次搜索,发现“保存”操作是使用独立存储实现的解决方案如下:
- 使用Clickonce安装
- 创建新域
- 修改注册表。
ClickOnce
并且不能修改注册表,因此我修改了源代码,创建了一个新的域。Imports DocumentFormat.OpenXml.Packaging
Imports System.IO
<Serializable()> Public Class ToIsolatedPackageSave
Public Sub Save(ByRef mainPart As MainDocumentPart, ByRef xmlDocument As Xml.XmlDocument)
Dim stream As IO.Stream
stream = mainPart.GetStream(FileMode.Create, FileAccess.Write)
xmlDocument.Save(stream) -----> same exception
End Sub
End Class
并且
Dim stream As Stream
Dim isolatedPackageSave As ToIsolatedPackageSave
Dim isolatedAppDomain As AppDomain
Try
Dim isolatedAppDomainSetup As AppDomainSetup = New AppDomainSetup()
isolatedAppDomainSetup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory
Dim isolatedEvidence As Evidence = New Evidence(AppDomain.CurrentDomain.Evidence)
isolatedEvidence.AddAssembly(Reflection.Assembly.GetExecutingAssembly().FullName)
isolatedEvidence.AddHost(New Zone(Security.SecurityZone.MyComputer))
isolatedAppDomain = AppDomain.CreateDomain("TrustIsolatedDomain", isolatedEvidence, isolatedAppDomainSetup)
isolatedPackageSave = isolatedAppDomain.CreateInstanceAndUnwrap(GetType(ToIsolatedPackageSave).Assembly.FullName, GetType(ToIsolatedPackageSave).FullName)
'(IsolatedPackageSave)isolatedAppDomainSetup.CreateInstanceAndUnwrap(GetType(ToIsolatedPackageSave).Assembly.FullName, GetType(ToIsolatedPackageSave).FullName)
isolatedPackageSave.Save(mainPart, MainXMLDoc)
Catch ex As Exception
Finally
AppDomain.Unload(isolatedAppDomain)
End Try
但是这段代码并没有解决我的问题。
IsolatedStorageFile
实现了IDisposable
接口,我建议使用using (var usfdAttempt1 = IsolatedStorageFile.GetUserStoreForDomain()) {}
。 - Heinzi