Altova XMLSpy 2024 Professional Edition

Sie können mit Hilfe von C# auf die Funktionalitäten der Application API zugreifen. Zur Erstellung des C#-Code können Sie Visual Studio 2012/2013/2015/2017/2019/2022 verwenden und den Code in einem Visual Studio-Projekt speichern. Erstellen Sie das Projekt folgendermaßen:

 

1.Fügen Sie in Microsoft Visual Studio mit dem Befehl Datei | Neu | Projekt ein neues Projekt hinzu.

2.Fügen Sie durch Auswahl des Befehls Projekt | Verweis hinzufügen hinzufügen eine Referenz zur XMLSpy-Typbibliothek hinzu. Daraufhin wird das Dialogfeld "Verweis hinzufügen" angezeigt. Navigieren Sie zur XMLSpy-Typbibliothekkomponente, die sich im XMLSpy-Applikationsordner befindet und fügen Sie sie hinzu.

3.Geben Sie den gewünschten Code ein.

4.Kompilieren Sie den Code und führen Sie ihn aus.

 

C#-Beispielprojekt

Das XMLSpy-Paket enthält ein C#-Beispielprojekt, das Sie im Unterordner API\C# des Ordners Examples finden:

 

 

Windows 7, Windows 8, Windows 10, Windows 11

C:\Benutzer\<Benutzername>\Dokumente\
Altova\XMLSpy\2024\Examples

 

Sie können das Projekt innerhalb von Visual Studio 2012/2013/2015/2017/2019/2022 kompilieren und ausführen.

 

Anhand der unten stehenden Codefragmente wird gezeigt, wie Sie Grundfunktionen der Applikation verwenden können. Der Code ist dem Code im C#-Beispielprojekt im Ordner API Examples Ihres Applikationsordners ähnlich, kann sich aber geringfügig davon unterscheiden.

 

Platformkonfiguration

Wenn Sie ein 64-Bit-Betriebssystem haben und eine 32-Bit-Installation von XMLSpy verwenden, müssen Sie die x86-Plattform im Konfigurationsmanager der Projektmappe hinzufügen und das Beispiel mit dieser Konfiguration erstellen. Im Dialogfeld "Neue Projektmappenplattform" kann eine neue x86-Plattform (für die aktive Projektmappe in Visual Studio) erstellt werden (Build | Konfigurations-Manager | Aktive Projektmappenplattform: | <Neu…>).

 

Funktionen im Codebeispiel unten

In diesem Beispiel sehen Sie eine einfache Benutzeroberfläche (Abbildung unten) mit Schaltflächen, über die grundlegende XMLSpy-Operationen aufgerufen werden:

 

CSInterfaceForm

 

 

Start XMLSpy: Startet XMLSpy, das als Automation Server registriert ist, oder aktiviert das Programm, wenn XMLSpy bereits ausgeführt wird.

Open OrgChart.pxf: Navigiert zu einem der mit XMLSpy installierten Beispieldokumente und öffnet es. Wenn das Dokument bereits offen ist, wird es zum aktiven Dokument.

OnDocumentOpened Event On/Off: Zeigt, wie die Applikation auf XMLSpy Events hört. Ist die Funktion aktiv, erscheint ein Meldungsfeld, nachdem ein Dokument geöffnet wurde.

Open ExpReport.xml: Öffnet ein weiteres Beispieldokument.

Toggle View Mode: Wechselt in der Ansicht aller offenen Dokumente zwischen der Text- und der Authentic-Ansicht. Im Code wird gezeigt, wie Sie durch offene Dokumente iterieren können.

Validate: Validiert das aktive Dokument und zeigt das Ergebnis in einem Meldungsfeld an. Im Code wird gezeigt, wie Fehler und COM-Ausgabeparameter behandelt werden.

Shutdown XMLSpy: Beendet XMLSpy.

 

 

Sie können den Code (des Codefragments unten oder des C#-Beispiels aus dem Ordner API Examples) beliebig modifizieren und ausführen.

 

Kompilieren und Ausführen des Beispiels

Doppelklicken Sie im Ordner API Examples auf die Datei AutomateXMLSpy_VS2008.sln bzw. auf die Datei AutomateXMLSpy_VS2010.sln (um sie in Visual Studio 2012/2013/2015/2017/2019/2022 zu öffnen). Alternativ dazu können Sie die Datei auch von Visual Studio aus öffnen (mit dem Befehl Datei | Öffnen | Projekt/Projektmappe). Um das Beispiel zu kompilieren und auszuführen wählen Sie Debuggen | Debuggen starten bzw. Debuggen | Starten ohne Debugging.

 

Code des Beispiels

Nachstehend sehen Sie den C#-Code der Grundfunktionen des im AutomateXMLSpy Beispiel erstellten Formulars (Form1.cs). Beachten Sie, dass ich der unten aufgelistete Code geringfügig von dem Code im Formular unter API Examples unterscheiden kann. Der Code ist zum besseren Verständnis mit Kommentaren versehen. Abhängig davon, welche Application API-Funktionen sie aufrufen, sind Teile dieses Codes auch separat in Unterabschnitten dieses Abschnitts beschrieben.

 

Der Code besteht im Wesentlichen aus einer Reihe von Handlern für die Schaltflächen der oben gezeigten Benutzeroberfläche.

 

 

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