Generating a class from XSD at runtime

How do I create a C # class from XSD at runtime?

Also, how does it list the properties that are contained in the type?

+3


source to share


1 answer


It's definitely possible .. and not too difficult. You just need to add a few different methods. You can use the Description Importer to import service descriptions at runtime. Link

Basically I created the following steps:

1) Get the WSDL file using a reader (local or remote, different approaches)

XmlTextReader myXmlReader;
myWebService = new WebServiceImporterCompiler(WSDLPath, soapVersion);

if (useLocalWSDL)
{
     FileWebRequest wr = (FileWebRequest)FileWebRequest.Create(WSDLPath);
     FileWebResponse wres = (FileWebResponse)wr.GetResponse();
     myXmlReader = new XmlTextReader(wres.GetResponseStream());
}
else
{
     Uri uri = new Uri(WSDLPath); //WEBSERVICE URI
     HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create(uri.OriginalString + "?wsdl");
     wr.Credentials = wr.Credentials = new NetworkCredential(userName, password ?? "");
     HttpWebResponse wres = (HttpWebResponse)wr.GetResponse();
     myXmlReader = new XmlTextReader(wres.GetResponseStream());
}

      

2) Create assembly from definition / myXmlReader

Check if xml is readable

if (!System.Web.Services.Description.ServiceDescription.CanRead(myXmlReader)) 
{
           throw new IOException("WSDL not readable");
}

      

Load the importer with some basic parameters (you can add / change something here) I am creating an assembly (dll) but with the switch parameter .GenerateInMemory you will be able to generate the class in memory.



ServiceDescriptionImporter descriptionImporter = new ServiceDescriptionImporter();
ServiceDescription serviceDescription = ServiceDescription.Read(myXmlReader);
descriptionImporter.ProtocolName = soapVersion.ToString(); // EITHER SOAP OR SOAP12
descriptionImporter.AddServiceDescription(serviceDescription, null, null);
descriptionImporter.Style = ServiceDescriptionImportStyle.Client;
descriptionImporter.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties;

      

Compile the assembly using the CodeDomProvider

CodeCompileUnit codeUnit = new CodeCompileUnit();
CodeNamespace codeNamespace = new CodeNamespace();
codeUnit.Namespaces.Add(codeNamespace); // Add additional Namespaces
ServiceDescriptionImportWarnings importWarnings = descriptionImporter.Import(codeNamespace, codeUnit);
if (importWarnings == 0)
{
      using (CodeDomProvider compiler = CodeDomProvider.CreateProvider("CSharp"))
      {
           string[] references = { "System.dll", "System.Web.Services.dll", "System.Xml.dll" };
           CompilerParameters parameters = new CompilerParameters(references); 
           parameters.GenerateExecutable = false;
           parameters.GenerateInMemory = false;
           parameters.IncludeDebugInformation = false;
           parameters.CompilerOptions = "/optimize";
           parameters.TempFiles = new TempFileCollection(System.IO.Path.GetTempPath() + "xxx", false);
           parameters.ReferencedAssemblies.Add("System.dll");

           results = compiler.CompileAssemblyFromSource(parameters, CSharpCode);
           foreach (CompilerError cError in results.Errors)
           {
               // log errors
           }
           if (results.Errors.Count > 0 || results.CompiledAssembly == null) throw new Exception("Kompilierfehler bei Assemblyerstellung");
       }
 }

      

3) Use the generated collector object to call methods, for example to call a service

public T InvokeMethod <T>(Assembly assembly, string serviceNameToCall, MethodInfo methodToCall)
{
        SoapHttpClientProtocol mySoapProtocoll;
    try
    {
         object serviceInstance = myAssembly.CreateInstance(serviceNameToCall);
         mySoapProtocoll = (SoapHttpClientProtocol)serviceInstance;
         mySoapProtocoll.Credentials = CredentialCache.DefaultCredentials; // or use your own
         object myObject = (T)ServiceType.InvokeMember(methodToCall, BindingFlags.InvokeMethod, null, mySoapProtocoll, args);
    }
}

      

To get the objects / available methods of methodInfo, use reflections to iterate over the assembly / classes.

A complete guide to reflections can be found here

0


source







All Articles