public void ImportRuleSets()
{
try
{
string title = "RuleSets";
string shortName = System.IO.Path.GetFileNameWithoutExtension( this.document.FullName);
string fileName = System.IO.Path.Combine( this.document.Path, shortName + ".xml");
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Title = "Import " + title;
dlg.InitialDirectory = System.Environment.GetFolderPath( System.Environment.SpecialFolder.MyDocuments);
dlg.DefaultExt = ".xml";
dlg.Filter = "XML documents (.xml)|*.xml |Visio XML drawing (.vdx)|*.vdx|Visio XML template (.vtx)|*.vtx";
if (dlg.ShowDialog() == true)
{
fileName = dlg.FileName;
}
else return;
XDocument xdoc = XDocument.Load(fileName);
XNamespace xns = "http://schemas.microsoft.com/visio/2003/core";
XNamespace v14 = "http://schemas.microsoft.com/office/visio/2009/5/extension";
XNamespace vx = "http://schemas.microsoft.com/visio/2006/extension";
XElement docNode = xdoc.Element(xns + "VisioDocument");
if (docNode == null) return;
//Get the Validation element (abort if none found)
XElement validNode = docNode.Element(v14 + "Validation");
if (validNode == null) return;
//Get the RuleSets element (abort if none found)
XElement ruleSetsNode = validNode.Element(v14 + "RuleSets");
if (ruleSetsNode == null) return;
foreach (XElement ruleSetNode in ruleSetsNode.Elements(v14 + "RuleSet"))
{
//Get the NameU attribute
string rsName = ruleSetNode.Attribute("NameU").Value;
//Set the default response
System.Windows.MessageBoxResult process = System.Windows.MessageBoxResult.Yes;
//Check if the rule set exists already
if (this.VERuleSets.Count(ver => ver.NameU == rsName) >0)
{
rules setsimporting, from XML//Ask to replace an existing ruleset (or skip if declined)
process = System.Windows.MessageBox.Show( "The rule set, " + rsName + ", exists already.\\nDo you wish to replace it?",
"Import Ruleset", System.Windows.MessageBoxButton.YesNo, System.Windows.MessageBoxImage.Question, System.Windows.MessageBoxResult.Yes);
if (process == System.Windows.MessageBoxResult.No) break;
rule sets, importing from XMLthis.VERuleSets[rsName].Delete();
}
else process = System.Windows.MessageBoxResult.Yes;
//Add a new VERuleSet object to this VEDocument
VERuleSet vrset = this.VERuleSets.AddRuleSet(rsName);
//Set the properties of the VERuleSet from the attributes
foreach (XAttribute xat in ruleSetNode.Attributes())
{
switch (xat.Name.LocalName)
{
case "Name":
vrset.Name = xat.Value;
break;
case "Description":
vrset.Description = xat.Value;
break;
case "RuleSetFlags":
vrset.RuleSetFlags = (Visio.VisRuleSetFlags)Convert.ToInt32( xat.Value);
break;
}
}
//Set the remaining properties of the VERuleSet from the elements
foreach (XElement xelm in ruleSetNode.Elements())
{
switch (xelm.Name.LocalName)
{
case "Rule":
string rName = xelm.Attribute("NameU").Value;
VERule vrle = vrset.VERules.AddRule(rName);
//Set the properties of the VERule from the attributes
foreach (XAttribute xat in xelm.Attributes())
{
switch (xat.Name.LocalName)
{
rules setsimporting, from XMLcase "Category":
vrle.Category = xat.Value;
break;
case "Description":
vrle.Description = xat.Value;
break;
case "TargetType":
vrle.TargetType = (Visio.VisRuleTargets)Convert.ToInt32( xat.Value);
break;
rule sets, importing from XML}
}
//Set the remaining properties of the VERule from the elements
foreach (XElement xelmR in xelm.Elements())
{
switch (xelmR.Name.LocalName)
{
case "RuleFilter":
vrle.FilterExpression = xelmR.Value;
break;
case "RuleTest":
vrle.TestExpression = xelmR.Value;
break;
}
}
break;
}
}
}
}
catch (Exception)
{
throw;
}
rules setsimporting, from XML}