Using SolutionXML with C#

Visio has a feature which allows third-party solutions to store it’s custom data in Visio files as XML fragments. It might be useful if you don’t want to litter document or shapes shapesheet(s) with your data. Also it has an advantage that setting it, unlike setting cells in shapesheet, does not trigger undo/redo, so user’s work is not interfered.

Working with solution XML elements might require a bit work, here is a short code fragment how to deal with that in C#. The key point is that you need to create your own namespace in the XML, and then all your data should go there. The sample assumes that you want to store some “settings” in the solution xml element.

Here is a reusable class that can be used to store/load settings in solution xml element in Visio document, if you are using C#

    // contains your solution's settings
    [Serializable]
    public class Settings
    {
        public string SettingOne { get; set; }
        public int SettingTwo { get; set; }
    }

    // helper class to save/load settings to/from Visio document
    public class SettingsManager
    {
        // modify to suit your needs
        private const string SolutionXmlNamespace = "http://yournamespace.com/";

         // modify to suit your needs
        private const string SolutionXmlElementName = "YourElementName";

        [XmlRoot("SolutionXML")]
        public class SolutionXmlElement
        {
            [XmlAttribute]
            public string Name { get; set; }

            [XmlElement(Namespace = SolutionXmlNamespace)]
            public Settings Settings { get; set; }
        }

        private static Settings Load(Document doc)
        {
            if (doc == null)
                return null;

            if (!doc.SolutionXMLElementExists[SolutionXmlElementName])
                return null;

            var xml = doc.SolutionXMLElement[SolutionXmlElementName];

            if (xml == null)
                return null;

            using (var sr = new StringReader(xml))
            {
                var serializer = new XmlSerializer(typeof(SolutionXmlElement));

                var solutionXml = serializer.Deserialize(sr) as SolutionXmlElement;
                if (solutionXml == null)
                    return null;

                return solutionXml.Settings;
            }
        }

        public static void Store(Document doc, Settings options)
        {
            if (doc == null)
                return;

            var solutionXml = new SolutionXmlElement
            {
                Name = SolutionXmlElementName,
                Settings = options
            };

            var ns = new XmlSerializerNamespaces();
            ns.Add("s", SolutionXmlNamespace);

            using (var sw = new StringWriter())
            {
                var xw = XmlWriter.Create(sw, new XmlWriterSettings
                {
                    OmitXmlDeclaration = true
                });

                var serializer = new XmlSerializer(typeof(SolutionXmlElement));

                serializer.Serialize(xw, solutionXml, ns);

                var xml = sw.ToString();
                doc.SolutionXMLElement[SolutionXmlElementName] = xml;
            }
        }
    }
   
    //or whatever...
    var doc = visioApplication.ActiveDocument; 

    // read:
    var mySettings = SettingsManager.Load(doc);

     // write:
    SettingsManager.Store(doc, mySettings);

Leave a Reply