.NET Example: C#

www.altova.com Print this Topic Previous Page Up One Level Next page

Home >  Server APIs: HTTP REST, COM/.NET, Java > COM/.NET API >

.NET Example: C#

The C# example below does the following:

 

Set up and initialize the RaptorXML .NET object
Validate an XML file
Perform an XSLT transformation, return the result as a string
Process an XQuery document, save the result in a file
Set up the execution sequence of the code and its entry point

 

 

using System;

using System.Text;

using Altova.RaptorXMLServer;

 

namespace RaptorXMLRunner

{

  class Program

   {

  // The RaptorXML Server .NET object

      static ServerClass objRaptorXMLServer;

 

  // Initialize the RaptorXML Server .NET object

      static void Init()

      {

      // Allocate a RaptorXML Server object

         objRaptorXMLServer = new ServerClass();

 

      // Configure the server: error reporting, HTTP server name and port

      // (IPv6 localhost in this example)

         objRaptorXMLServer.ErrorLimit = 1;

         objRaptorXMLServer.ReportOptionalWarnings = true;

         objRaptorXMLServer.ServerName = "::1"

         objRaptorXMLServer.ServerPort = 8087

      }

 

 

      // Validate one file

        static void ValidateXML()

         {

        // Get a validator engine instance from the Server object

            XMLValidator objXMLValidator = objRaptorXMLServer.GetXMLValidator();

 

        // Configure input data

            objXMLValidator.InputFileName = "MyXMLFile.xml";

 

        // Validate; in case of invalid file,

            report the problem returned by RaptorXML

            if ( objXMLValidator.IsValid() )

               Console.WriteLine( "Input string is valid" );

            else

               Console.WriteLine( objXMLValidator.LastErrorMessage );

         }

 

 

 

      // Perform an XSLT transformation, and

      // return the result as a string

        static void RunXSLT()

         {

        // Get an XSLT engine instance from the Server object

            XSLT objXSLT = objRaptorXMLServer.GetXSLT();

 

            // Configure input data

               objXSLT.InputXMLFileName = "MyXMLFile.xml";

               objXSLT.XSLFileName = "MyTransformation.xsl";

 

          // Run the transformation.

           // In case of success, the result is returned.

           // In case of errors, an error listing

              Console.WriteLine( objXSLT.ExecuteAndGetResultAsString() );

         }

 

 

 

      // Execute an XQuery, save the result in a file

        static void RunXQuery()

         {

        // Get an XQuery engine instance from the Server object

            XQuery objXQuery = objRaptorXMLServer.GetXQuery();

 

          // Configure input data

              objXQuery.InputXMLFileName = exampleFolder + "simple.xml";

              objXQuery.XQueryFileName = exampleFolder + "CopyInput.xq";

 

          // Configure serialization (optional, for better formatting)

             objXQuery.OutputEncoding = "UTF8"

             objXQuery.OutputIndent = true

             objXQuery.OutputMethod = "xml"

             objXQuery.OutputOmitXMLDeclaration = false

 

          // Run the query; result serialized to given path

             objXQuery.Execute( "MyQueryResult.xml" );

         }

 

 

 

        static void Main(string[] args)

         {

          try

           {

          // Entry point. Perform all functions

              Init();

              ValidateXML();

              RunXSLT();

              RunXQuery();

           }

          catch (System.Exception ex)

           {

               Console.WriteLine( ex.Message );

               Console.WriteLine( ex.ToString() );

           }

       }

   }

}

 

 


© 2019 Altova GmbH