Altova XMLSpy 2023 Professional Edition

The C# programming language can be used to access the Application API functionality. You could use Visual Studio 2012/2013/2015/2017/2019/2022 to create the C# code, saving it in a Visual Studio project. Create the project as follows:

 

1.In Microsoft Visual Studio, add a new project using File | New | Project.

2.Add a reference to the XMLSpy Type Library by clicking Project | Add Reference. The Add Reference dialog appears. Browse for the XMLSpy Type Library component, which is located in the XMLSpy application folder, and add it.

3.Enter the code you want.

4.Compile the code and run it.

 

Example C# project

Your XMLSpy package contains an example C# project, which is located in the API\C# subfolder of the Examples folder :

 

 

Windows 7, Windows 8, Windows 10

C:\Users\<username>\Documents\
Altova\XMLSpy\2023\Examples

 

You can compile and run the project from within Visual Studio 2012/2013/2015/2017/2019/2022. The code listing below shows how basic application functionality can be used. This code is similar to the example C# project in the API Examples folder of your application package, but might differ slightly.

 

Platform configuration

If  you have a 64-bit operating system and are using a 32-bit installation of XMLSpy, you must add the x86 platform in the solution's Configuration Manager and build the sample using this configuration. A new x86 platform (for the active solution in Visual Studio) can be created in the New Solution Platform dialog (Build | Configuration Manager | Active solution platform | <New…>).

 

What the code listing below does

The example code listing below creates a simple user interface (screenshot below) with buttons that invoke basic XMLSpy operations:

 

CSInterfaceForm

 

Start XMLSpy: Starts XMLSpy, which is registered as an automation server, or activates the application if it is already running.

Open OrgChart.pxf: Locates one of the example documents installed with XMLSpy and opens it. If this document is already open it becomes the active document.

OnDocumentOpened Event On/Off: Shows how to listen to XMLSpy events. When turned on, a message box will pop up after a document has been opened.

Open ExpReport.xml: Opens another example document.

Toggle View Mode: Changes the view of all open documents between Text View and Authentic View. The code shows how to iterate through open documents.

Validate: Validates the active document and shows the result in a message box. The code shows how to handle errors and COM output parameters.

Shut down XMLSpy: Stops XMLSpy.

 

You can modify the code (of the code listing below or of the example C# project in the API Examples folder) in any way you like and run it.

 

Compiling and running the example

In the API Examples folder, double-click the file AutomateXMLSpy_VS2008.sln or the file AutomateXMLSpy_VS2010.sln (to open in Visual Studio 2012/2013/2015/2017/2019/2022). Alternatively the file can be opened from within Visual Studio (with File | Open | Project/Solution). To compile and run the example, select Debug | Start Debugging or Debug | Start Without Debugging.

 

Code listing of the example

Given below is the C# code listing of the basic functionality of the form (Form1.cs) created in the AutomateXMLSpy example. Note that the code listed below might differ slightly from the code in the API Examples form.The listing below is commented for ease of understanding. Parts of the code are also presented separately in the sub-sections of this section, according to the Application API functionality they access.

 

The code essentially consists of a series of handlers for the buttons in the user interface shown in the screenshot above.

 

 

namespace WindowsFormsApplication2

{

   public partial class Form1 : Form

   {

       public Form1()

       {

           InitializeComponent();

       }

 

       // An instance of XMLSpy accessed via its automation interface

       XMLSpyLib.Application XMLSpy;

 

       // Location of examples installed with XMLSpy

       String strExamplesFolder;

 

       private void Form1_Load(object sender, EventArgs e)

       {

           // Locate examples installed with XMLSpy

           // REMARK: You might need to adapt this if you have a different major version of the product

           strExamplesFolder = Environment.GetEnvironmentVariable("USERPROFILE") + "\\My Documents\\Altova\\XMLSpy2012\\Examples\\";

       }

 

       // Handler for the "Start XMLSpy" button

       private void StartXMLSpy_Click(object sender, EventArgs e)

       {

           if (XMLSpy == null)

           {

               Cursor.Current = Cursors.WaitCursor;

 

               // If no XMLSpy instance is open, create one and make it visible

               XMLSpy = new XMLSpyLib.Application();

               XMLSpy.Visible = true;

 

               Cursor.Current = Cursors.Default;

           }

           else

           {

               // If an instance of XMLSpy is already running, make sure it's visible

               if (!XMLSpy.Visible)

                   XMLSpy.Visible = true;

           }

       }

 

       // Handler for the "Open OrgChart.pxf" button

       private void openOrgChart_Click(object sender, EventArgs e)

       {

           // Make sure there's a running XMLSpy instance, and that it's visible

           StartXMLSpy_Click(null, null);

 

           // Open one of the example files installed with the product

           XMLSpy.Documents.OpenFile(strExamplesFolder + "OrgChart.pxf", false);

       }

 

       // Handler for the "Open ExpReport.xml" button

       private void openExpReport_Click(object sender, EventArgs e)

       {

           // Make sure there's a running XMLSpy instance, and that it's visible

           StartXMLSpy_Click(null, null);

 

           // Open one of the sample files installed with the product.

           XMLSpy.Documents.OpenFile(strExamplesFolder + "ExpReport.xml", false);

       }

 

       // Handler for the "Toggle View Mode" button

       private void toggleView_Click(object sender, EventArgs e)

       {

           // Make sure there's a running XMLSpy instance, and that it's visible

           StartXMLSpy_Click(null, null);

 

           // Iterate through all open documents and toggle view between Text View and Authentic View

           foreach (XMLSpyLib.Document doc in XMLSpy.Documents)

               if (doc.CurrentViewMode == XMLSpyLib.SPYViewModes.spyViewText)

                   doc.SwitchViewMode(XMLSpyLib.SPYViewModes.spyViewAuthentic);

               else

                   doc.SwitchViewMode(XMLSpyLib.SPYViewModes.spyViewText);

       }

 

       // Handler for the "Shutdown XMLSpy" button

       // Shut down the application instance by explicitly releasing the COM object

       private void shutdownXMLSpy_Click(object sender, EventArgs e)

       {

           if (XMLSpy != null)

           {

               // Allow shutdown of XMLSpy by releasing the UI

               XMLSpy.Visible = false;

 

               // Explicitly release the COM object

               try

               {

                   while (System.Runtime.InteropServices.Marshal.ReleaseComObject(XMLSpy) > 0) ;

               }

               finally

               {

                   // Disallow subsequent access to this object

                   XMLSpy = null;

               }

           }

       }

 

       // Handler for button "Validate"

       private void validate_Click(object sender, EventArgs e)

       {

           // COM errors are returned to C# as exceptions. We use a try/catch block to handle them.

           try

           {

               // Method 'IsValid' is one of the few functions that uses output parameters

               // Use 'object' type for these parameters

               object strErrorText = "";

               object nErrorNumber = 0;

               object errorData = null;

 

               if (!XMLSpy.ActiveDocument.IsValid(ref strErrorText, ref nErrorNumber, ref errorData))

               {

                   // The COM call succeeded but the document is not valid

                   // A detailed description of the problem is returned in strErrorText, nErrorNumber and errorData

                   listBoxMessages.Items.Add("Document " + XMLSpy.ActiveDocument.Name + " is not valid.");

                   listBoxMessages.Items.Add("\tErrorText  : " + strErrorText);

                   listBoxMessages.Items.Add("\tErrorNumber: " + nErrorNumber);

                   listBoxMessages.Items.Add("\tElement    : " + (errorData != null ? ((XMLSpyLib.XMLData)errorData).TextValue : "null"));

               }

               else

               {

                   // The COM call succeeded and the document is valid

                   listBoxMessages.Items.Add("Document " + XMLSpy.ActiveDocument.Name + " is valid.");

               }

           }

           catch (Exception ex)

           {

               // The COM call was not successful

               // Probably no application instance has been started or no document is open.

               listBoxMessages.Items.Add("Error validating active document: " + ex.Message);

           }

       }

 

       

 

       // Event handler for OnDocumentOpened event

       private void handleOnDocumentOpened(XMLSpyLib.Document i_ipDocument)

       {

           MessageBox.Show("Document " + i_ipDocument.Name + " was opened!");

       }

 

       // Remember if the event handler is currently registered.

       private bool bEventHandlerIsRegistered = false;

 

       // Handler for button 'OnDocuemntOpened Event On/Off

       private void toggleOnDocumentOpenedEvent_Click(object sender, EventArgs e)

       {

           if (XMLSpy != null)

           {

               if (bEventHandlerIsRegistered)

                   XMLSpy.OnDocumentOpened -= new XMLSpyLib._IApplicationEvents_OnDocumentOpenedEventHandler(handleOnDocumentOpened);

               else

                   XMLSpy.OnDocumentOpened += new XMLSpyLib._IApplicationEvents_OnDocumentOpenedEventHandler(handleOnDocumentOpened);

 

               bEventHandlerIsRegistered = !bEventHandlerIsRegistered;

           }

       }

   }

}

 

© 2017-2023 Altova GmbH