Even though there is an option to import a rule set
from another Visio document, I know that some rules developers would
like to export and import rule sets to XML. This allows rule sets to be
stored, restored, and analyzed more easily.
I decided that the XML
structure exported should mimic the Visio XML format, and thus use a
part of the Visio XML schema. This means using the same namespaces, but
it would mean that any XSL stylesheets developed for our export would
also work for the standard Visio XML format (*.vdx and *.vtx files).
I decided to include the
option to export the issues in a document too, because someone may have
the need to use them in an external program. Having the issues available
in XML format means that they could be displayed as a table, for
example, so that they can be reviewed independently.
The ExportDocument() method first constructs a title for SaveFile dialog, depending upon the include options provided. The default name preferred for the XML file uses the drawing file name as a base.
Once a file name has been obtained, the System.XMl.Linq.XDocument object is created; saved and opened in the associated application.
public void ExportDocument(bool includeRulesets, bool includeIssues)
{
try
{
//Set the title for the SaveFile dialog
string title = "";
if (includeRulesets) title += "RuleSets";
if (includeRulesets && includeIssues) title += " and ";
if (includeIssues) title += "Issues";
string shortName = System.IO.Path.GetFileNameWithoutExtension( this.document.FullName);
string fileName = System.IO.Path.Combine(this.document.Path, shortName + ".xml");
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.Title = "Save " + title;
dlg.InitialDirectory = System.Environment.GetFolderPath( System.Environment.SpecialFolder.MyDocuments);
dlg.FileName = shortName + " " + title + ".xml";
dlg.OverwritePrompt = true;
dlg.DefaultExt = ".xml";
dlg.Filter = "XML documents (.xml)|*.xml";
if (dlg.ShowDialog() == true)
{
fileName = dlg.FileName;
}
else return;
XDocument xDoc = getXDocument(includeRulesets, includeIssues);
if (xDoc != null)
rules setsexporting, to XML{
//Save the file
xDoc.Save(fileName);
//Open the file with the associated program
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(fileName);
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
System.Diagnostics.Process.Start(startInfo);
}
Visio diagrams, annotatingrule sets, exporting to XML}
catch (Exception)
{
throw;
}
}
Getting the XDocument object
The following method first creates the required XNamespace objects, then it creates a new XDocument object and retrieves the XElement objects for the VERules and/or VEIssues of the VEDocument.
private XDocument getXDocument(bool includeRulesets, bool includeIssues)
{
try
{
//v14:Validation
// v14:ValidationPoperties
// LastValidated
// ShowIgnored
// v14:RuleSets
// v14:RuleSet
// ID
// NameU
// Description
// v14:Rule
// ID
// NameU
// Category
// Description
// v14:RuleFilter
// v14:RuleTest
// v14:Issues
// v14:Issue
// ID
// v14:IssueTarget
// PageID
// ShapeID
// v14:RuleInfo
// RuleSetID
// RuleID
XNamespace xns = "http://schemas.microsoft.com/visio/2003/core";
rule sets, exporting to XMLXDocument object, obtainingXNamespace v14 = "http://schemas.microsoft.com/office/visio/2009/5/extension";
XNamespace vx = "http://schemas.microsoft.com/visio/2006/extension";
XDocument xdoc = new XDocument( new XDeclaration("1.0", "utf-8", "yes"),
new XComment("Exported from Rules Tools " + this.document.Name + " on " + System.DateTime.Now.ToUniversalTime().ToString()),
new XElement(xns + "VisioDocument", new XAttribute(XNamespace.Xmlns + "vx", vx.NamespaceName),
new XAttribute(XNamespace.Xmlns + "v14", v14.NamespaceName), new XElement(v14 + "Validation")));
XElement validNode = xdoc.Element(xns + "VisioDocument").Element(v14 + "Validation");
if (includeRulesets)
{
if (this.SelectedVERuleSet == null)
{
validNode.Add(new XElement(v14 + "RuleSets",
from el in this.VERuleSets
select el.GetXElement(v14)
));
}
else
{
validNode.Add(new XElement(v14 + "RuleSets",
from el in this.VERuleSets
where (el.ID == this.selectedVERuleSet.ID)
select el.GetXElement(v14)
));
rule sets, exporting to XMLXDocument object, obtaining}
}
if (includeIssues)
{
validNode.Add(new XElement(v14 + "Issues",
from el in this.VEIssues
select el.GetXElement(v14)
));
}
return xdoc;
}
catch (Exception)
{
}
return null;
}
Getting the VERuleSet XElement
The following method creates an XElement for the VERuleSet object, and then adds an XElement for each VERule in the VERules collection.
public XElement GetXElement(XNamespace v14)
{
XElement retNode;
try
{
retNode = new XElement(v14 + "RuleSet",
new XAttribute("ID", this.ID),
new XAttribute("NameU", this.NameU),
new XAttribute("Name", this.Name),
new XAttribute("Description", this.Description));
retNode.Add(from ver in this.VERules select ver.GetXElement(v14));
}
catch (Exception)
{
throw;
}
return retNode;
}
Getting the VEIssue XElement
This method creates an XElement for the VEIssue object, and then adds an XElement for the RuleInfo and IssueTarget.
public XElement GetXElement(XNamespace v14)
{
XElement retNode;
try
{
retNode = new XElement(v14 + "Issue", new XAttribute("ID", this.ID),
new XElement(v14 + "RuleInfo", new XAttribute("RuleSet",this.Rule.RuleSet.ID), new XAttribute("Rule", this.Rule.ID)));
if (this.Ignored)
{
retNode.Add(new XAttribute("Ignored", this.Ignored));
}
if (this.TargetPage != null || this.TargetShape != null)
{
XElement targetNode = new XElement(v14 + "IssueTarget");
if (this.TargetPage != null)
targetNode.Add(new XAttribute("PageID", this.TargetPage.ID));
if (this.TargetShape != null)
targetNode.Add(new XAttribute("ShapeID", this.TargetShape.ID));
retNode.Add(targetNode);
}
}
catch (Exception)
{
throw;
}
return retNode;
}