.NET Example: C#
L'exemple C# ci-dessous permet les opérations suivantes :
•Configurer et initialiser l'objet .NET RaptorXML
•Effectuer une transformation XSLT, retourner le résultat en tant que string
•Traiter un document XQuery, enregistrer le résultat dans un fichier
•Configurer la séquence d'exécution du code et de son point d'entrée
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() );
}
}
}
}