我正在为Visio开发VSTO加载项,并且需要使用Document.SolutionXmlElement属性保存文档属性。以下代码演示了处理程序,该处理程序在保存之前将对象的XML序列化。基于阅读,我将C#类实例包装在具有名称属性的SolutionXml包装器中,该属性设置为封闭的序列化类实例的元素名称:
private void Document_Saved(Visio.Document document)
{
IDictionary<string, ModelGenMVCModel> documentModel = GetRibbon().documentModel;
if (documentModel.TryGetValue(document.Name, out ModelGenMVCModel modelGenMVCModel))
{
try
{
string serializedModel = new SolutionXml(modelGenMVCModel,ModelGenVisioAddIn.Properties.Resources.SolutionXmlElementName).Serialize();
LOGGER.Debug(String.Format("Saving model gen properties:\n{0}", serializedModel));
document.SolutionXMLElement[ModelGenVisioAddIn.Properties.Resources.SolutionXmlElementName] = serializedModel;
} catch ( Exception e)
{
LOGGER.Error(String.Format("Serialization error {0}\n{1}", e.Message, e.StackTrace));
}
}
}
serializzed XML如下:
<?xml version="1.0" encoding="utf-16"?>
<SolutionXml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Name="ModelGenMVCModel">
<ModelGenMVCModel>
<Author>Andrew Tyson</Author>
<UniqueID>Object_ID</UniqueID>
<Name>Name</Name>
<Description>Note</Description>
<LineColor/>
<FillColor/>
<SearchMatchLineColor/>
<SearchMatchFillColor/>
<SearchMatchFontColor/>
<SearchNoMatchLineColor/>
<SearchNoMatchFillColor/>
<LineWidth>1.5</LineWidth>
<PopupHeight>250</PopupHeight>
<PopupWidth>750</PopupWidth>
<InitialZoom>2</InitialZoom>
<MinZoom>0.05</MinZoom>
<MaxZoom>2</MaxZoom>
<ZoomSteps>0.05</ZoomSteps>
<EnablePan>true</EnablePan>
<EnableSearch>true</EnableSearch>
<DragPan>true</DragPan>
<MouseWheelZoom>true</MouseWheelZoom>
<DoubleClickZoom>true</DoubleClickZoom>
<PanDuration>300</PanDuration>
<PanAmount>100</PanAmount>
<OpenInBrowser>true</OpenInBrowser>
<SaveToLocation/>
</ModelGenMVCModel>
</SolutionXml>
但是我抛出了“无效参数”异常。任何帮助将不胜感激。
感谢和问候安德鲁
我这里有一个代码示例,演示如何管理SolutionXmlElement。您可以检查一下(如果它适合您)。它定义了一个简单的类来保存/加载设置:
https://unmanagedvisio.com/using-solutionxml-with-c/
我相信,问题可能是,您实际上需要在其中放置XML,而不是序列化的字符串。但是,如果不看完整的例子就很难说了。