RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


XMI: Using C# to Capture UML Models

UML models of the future will use XMI as a method of interchanging model data. And because XMI uses XML, suddenly the world is our oyster: In particular, you can extract model data and save it to a data store. Find out how to dissect data from your models using C#.

n my last article, I introduced XMI, an incredibly useful XML standard for interchanging models. Because XMI uses XML and XML is just text, using a schema or standard, it is perfectly suited for storing the model information in a database. As companies start managing IT as portfolios of applications they will need to use these models as a method for determining dependencies and managing their hardware assets in a graphical fashion.

There are a couple of different methods one can employ for capturing the XML model data and putting it into a data store, including, as you might expect, using XSLT. But I'm going to take a different approach, using the C# language. XSLT is a good choice for changing XML files but for more extensive applications that do more than transform data, C# or another high-level language like Java offer more flexibility.

In this article I will take the same ideas and samples from my earlier XMI article, and show how a UML deployment diagram can be dissected using XMI and C#. I'll begin with a simple deployment diagram of the servers within a fictional car leasing company and use C# to capture some data. This data can easily be added to a database (it is a relatively easy set of ADO calls) or as part of a larger asset based management system. With each example, I'll add complexity to the original diagram and the information being grabbed.

Example 1: Looking for Server Names

Figure 1. Five Servers: The nodes in this diagram represent the servers from the car leasing company. Each node is a unique server name.
The first thing I want to do is to capture the names of all the servers from the car leasing company. There are five servers, which are represented as a UML deployment diagram (see Figure 1). In the last article I showed how a deployment diagram can be represented as an XML document. To start the process the diagram is created and then exported to an XMI file (Enterprise Architect) or unzipped (MagicDraw). (For this article, again, I am using Enterprise Architect by SparxSystems.com.)

Note: In the last article I mentioned that Enterprise Architect stored each diagram as a separate XMI file but in fact it is by the project.

I will first develop a simple main program that prompts for the name of an XML document representing a valid XMI document. Once the file name is entered it is passed to an object that parses the XMI document and prints out the server names.

Figure 2. Output for Example 1: Each line in the output contains the server name followed by "->" and the XMI element id.

using System;
using System.Text;

namespace XMI_1
          public class ConsoleUtils
                    public static string ReadString(string msg)
                              return System.Console.ReadLine();

                    public static void WriteString(string msg)

                    public static void Main(string[] args)
                              string name = ReadString("Please enter the XMI filename : ");
                              NodeParse np = new NodeParse(name);
The next step is to parse the XMI document. To do this I have to create an instance of an XMLTextReader and loop until there are no more nodes. XMLNodeType.Element checks every XML element and, in the case where the element is a node, prints it out to the console. The element name is qualified as "UML:Node" however by using _readXMI.LocalName only the "Node" part of the element name is read. See Listing 1a for this portion of the code; the output is shown in Figure 2. (See Listing 1b for the complete source code for Example 1.)

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date