While I was working on a feature for an Enterprise Architect (EA) Add-In I had to get information about the scripts, which are stored in EA. The scripts are stored in table t_script
and the information I needed was Name
and Language
. Both properties reside inside the Notes
column.
Selecting Notes
of table t_script
with the following SQL query in SQL Scratch Pad of EA returns the Notes of the scripts.
SELECT [Notes]
FROM [t_script]
As you can see the XML structure of the values in the Notes
column is different. The XML structure depends on the type of entry. I identified three entry types:
- Custom scripts
- Script groups
- Built-in scripts
In my case I’m only interested in the custom scripts, so I changed the SQL query as follows. For built-in scripts the ScriptCategory
is set to ScriptDebugging
and for script groups the ScriptAuthor
is NULL.
SELECT [Notes]
FROM [t_script]
WHERE [ScriptCategory] <> 'ScriptDebugging'
AND [ScriptAuthor] IS NOT NULL
The XML structure of custom script notes is always the same.
<Script Name="DocScript" Type="Internal" Language="VBScript"/>
Based on that I implemented the data class for custom script notes.
/**
* Copyright 2020 d-fens GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System.Xml.Serialization;
namespace biz.dfch.CS.EA.Example
{
[XmlRoot("Script")]
public class ScriptNotesData : XmlSerialiserBase
{
[XmlAttribute]
public string Name { get; set; }
[XmlAttribute]
public string Type { get; set; }
[XmlAttribute]
public string Language { get; set; }
}
}
The ScriptNoteData
class extends the XmlSerialiserBase
class, which provides methods for serialisation and deserialisation using XmlSerializer
.
/**
* Copyright 2018 d-fens GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace biz.dfch.CS.EA.Example
{
public abstract class XmlSerialiserBase
{
protected const string XMLNS_FIELDS = " xmlns=\"Fields\"";
protected const string XMLNS_ROWS = " xmlns=\"Rows\"";
public virtual string Serialise()
{
string result;
var emptyNamespaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
var serializer = new XmlSerializer(GetType());
var settings = new XmlWriterSettings
{
Indent = false,
OmitXmlDeclaration = true,
Encoding = Encoding.UTF8,
};
using (var stream = new StringWriter())
using (var writer = XmlWriter.Create(stream, settings))
{
serializer.Serialize(writer, this, emptyNamespaces);
result = stream.ToString();
}
return result
.Replace(XMLNS_FIELDS, string.Empty)
.Replace(XMLNS_ROWS, string.Empty);
}
public static T Deserialise<T>(string xml)
where T : XmlSerialiserBase
{
var serializer = new XmlSerializer(typeof(T));
using (var stream = new StringReader(xml))
{
var result = (T) serializer.Deserialize(stream);
return result;
}
}
}
}
A single custom script note result can then be deserialised as follows.
var result = XmlSerialiserBase.Deserialise<ScriptNotesData>(scriptNotesEntry);