In the VS command prompt that is part of the VS SDK you can enter the following to generate an xsd file from an xml file:

xsd XMLfilename.xml

This will generate an xsd file with the same name as the xml file

Then you can enter:

xsd [.xsd file generated from above] /d /language:CS

This will generate a .cs file with the same name as the .xml and .xsd from above. In your program you can then load the xml file into a dataset in memory and query it programmatically using the class that was generated for that xml file.

I am searching for a way to perform the above steps programmatically rather than using the command prompt. Does anyone know how to do this?

Dani AI

Generated

Short summary and practical options (follow-up to and )

There are three practical ways to get the same .cs output that Xsd.exe produces, but from code:

  1. For strongly-typed DataSet output (closest match to the Xsd.exe “/d” behavior) use the framework TypedDataSetGenerator API. Load the schema into a DataSet, call the generator and write the returned source to a file. This runs inside your process and produces the DataSet-derived code. (learn.microsoft.com)

Example (full .NET Framework):

DataSet ds = new DataSet();
ds.ReadXmlSchema("schema.xsd");
CodeNamespace ns = new CodeNamespace("My.Namespace");
CSharpCodeProvider prov = new CSharpCodeProvider();
string source = TypedDataSetGenerator.Generate(ds, ns, prov);
File.WriteAllText("TypedDataSet.cs", source);
  1. If you need plain POCOs / XmlSerializer or WCF-style types, use the XSD -> Code DOM flow: load XmlSchema(s), use XmlSchemaImporter + XmlCodeExporter (or XsdDataContractImporter for WCF contracts) to get a CodeCompileUnit, then emit C# via a CodeDom provider. This gives you fine-grained control over namespaces, attributes and generation options. (learn.microsoft.com)

Minimal outline:

XmlSchema schema = XmlSchema.Read(...);
XmlSchemas xs = new XmlSchemas(); xs.Add(schema); xs.Compile(null);
XmlSchemaImporter imp = new XmlSchemaImporter(xs);
CodeNamespace codeNs = new CodeNamespace("Gen");
XmlCodeExporter exp = new XmlCodeExporter(codeNs);
... import type mappings and exp.ExportTypeMapping(...)
CSharpCodeProvider cp = new CSharpCodeProvider();
cp.GenerateCodeFromCompileUnit(compileUnit, writer, opts);
  1. If you just want the exact Xsd.exe output and don’t want to reimplement generation logic, call the Xsd.exe tool from your process (Process.Start) with the appropriate arguments. That’s simple and reliable but depends on the tool being installed. For modern/ cross-platform projects, consider third‑party generators like XmlSchemaClassGenerator. (learn.microsoft.com)

Notes: TypedDataSetGenerator and the XmlCodeExporter classes live in framework assemblies and are tied to the full .NET Framework; they may not exist on .NET Core/.NET 5+. Generating code from untrusted schemas can be risky—validate inputs before passing them to generators. (learn.microsoft.com)

Recommended Answers

All 2 Replies

are you asking for something like dataSet.Read/WriteXMLSchema methods ?

Yes that is similar, but I need the class (.cs file) that is auto-generated when using the command prompt. Can this be generated programmatically?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.