tags:

views:

269

answers:

1

Hello.

ParseXSD.cs

using System;
using System.Collections;
using System.Xml;
using System.Xml.Schema;


class XmlSchemaTraverseExample
{
    static void Main()
    {
        // Add the customer schema to a new XmlSchemaSet and compile it.
        // Any schema validation warnings and errors encountered reading or 
        // compiling the schema are handled by the ValidationEventHandler delegate.
        XmlSchemaSet schemaSet = new XmlSchemaSet();
        schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
        schemaSet.Add("http://www.w3.org/2001/XMLSchema", "tmp.xsd");
        schemaSet.Compile();

        // Retrieve the compiled XmlSchema object from the XmlSchemaSet
        // by iterating over the Schemas property.
        XmlSchema customerSchema = null;
        foreach (XmlSchema schema in schemaSet.Schemas())
        {
            customerSchema = schema;
        }

        // inserted more code here....
}

}

Currently, my ConsoleApp worked very well.

And i want to remove the hard code (xsd file path) out from my code below.

// i don't know how to update this line.

schemaSet.Add("http://www.w3.org/2001/XMLSchema", "tmp.xsd");

Then i could run my ParseXSD.cs file at the building time with the CSC command below.

// i don't know the correct command format. I could update the path parameter easily. NO hard code.

 CSC ParseXSD.cs d:/tmp/tmp.xsd

Please give me some guide. thanks in advance.

+2  A: 

Do you want to specify it at compile time or at run time?

to do it at runtime change

static void Main()

to

static void Main(string[] args)

and then

schemaSet.Add("http://www.w3.org/2001/XMLSchema", args[0]);

If you want to specify it at compile time you might be able to use precompile directives to do it, but I am not sure if they can be specified for csc. Your other option is to do the injection before compile with commandline scripts or similar.

GrayWizardx
Hello GrayWizardx, i want to specify it at compile time. The code will parse XSD file and generate a .CS file for run time usage. I am not familiar with CSC. that's why i ased the CSS parameters. There are some other perl script files during my application compile time. Could i combined the ParseXSD.cs file with perl script?
Nano HE
Sure. Replace this "tmp.xsd" with "$xsdFile$" then right before calling csc use your perl script to find that string and replace it with the desired xsd file location, then compile and execute. Though truthfully the above change would allow you to do this without need to recompile for each new xsd.
GrayWizardx