How to create a Class file from xml?

  • Thread starter Thread starter Brahm
  • Start date Start date
B

Brahm

Anyone,
How can i use xsd.exe to create a class file to include in my c#
project? Is this the right program to use? Doing it by hand is a pain.
thanks,
BRAHM
 
Hi Brahm:

From the command line you can run

XSD.exe /c schemafile.xsd

and it will produce a file with classes. Try xsd /? to see other
options available.
 
Brahm,

In order to do this, you will need to have a schema for your XML. If
you don't have one, XSD.EXE can do that for you, by passing an instance of
the XML to the tool. You would call it like this:

xsd.exe file.xml

This will create an XSD file which has the schema. You can then reinput
the file into XSD.EXE and create a class:

xsd.exe file.xsd /classes /language:cs

You can specify things like namespace as well, through other options on
the tool, but this is pretty much how you do it.

Hope this helps.
 
Instead of xsd.exe, consider using CodeSmith
http://www.ericjsmith.net/codesmith/

CodeSmith is a FREEWARE template-based code generator that can generate code
for any ASCII-based language. CodeSmith templates use a syntax nearly
identical to ASP.NET syntax so that creating templates should feel
immediately familiar to ASP.NET developers.
 
Here is the xml file and i tried following your steps but it is giving
me error.
<?xml version="1.0" encoding="utf-8"?>
<EngineeringData xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
messageType="Started/NewData" xmlns="urn:engineering:xsd.FE">
<Context>
<CV pName="ID">
<S>MyID</S>
</CV>
<CV pName="duct">
<S>MyDuct</S>
</CV>
<CV pName="out">
<S>Mute</S>
</CV>
<CV pName="Operate">
<S>Mop</S>
</CV>
<CV pName="Op">
<S>MyOp</S>
</CV>
<CV pName="work">
<S>N</S>
</CV>
<CV pName="Type">
<S>SND</S>
</CV>
<CV pName="Too">
<S>XTWToo</S>
</CV>
<CV pName="Pipe">
<S>PhillipsPipe</S>
</CV>
<CV pName="ticle">
<S>1x30ybstr0ma1</S>
</CV>
<CV pName="ParameterSettings">
<Arr pName="Setting">
<S>hello</S>
<F4>30.1</F4>
</Arr>
</CV>
<CV pName="DNA">
<S>934082</S>
</CV>
<CV pName="Map">
<Arr pName="dMap">
<F4>32</F4>
<F4>33</F4>
<I4>12</I4>
<I4>12</I4>
</Arr>
</CV>
</Context>
<Data>
<DV pName="Sequence">
<I2>1</I2>
</DV>
<DV pName="Number">
<I2>5</I2>
</DV>
<DV pName="Trial">
<Su pName="number"><I2>1</I2></Su>
<Su pName="row"><I2>1</I2></Su>
<Su pName="column"><I2>1</I2></Su>
<Su pName="Odometer"><I2>Y</I2></Su>
<Su pName="AZMN_MIN"><F4>30.1</F4></Su>
<Su pName="AZMN_MAX"><F4>30.1</F4></Su>
<Su pName="APMN_MIN"><F4>30.1</F4></Su>
<Su pName="APMN_MAX"><F4>30.1</F4></Su>
<Su pName="ARMN_MIN"><F4>30.1</F4></Su>
<Su pName="ARMN_MAX"><F4>30.1</F4></Su>
<Su pName="AXMN_MIN"><F4>30.1</F4></Su>
<Su pName="AXMN_MAX"><F4>30.1</F4></Su>
<Su pName="AYMN_MIN"><F4>30.1</F4></Su>
<Su pName="AYMN_MAX"><F4>30.1</F4></Su>
<Su pName="AZMD_MIN"><F4>30.1</F4></Su>
<Su pName="AZMD_MAX"><F4>30.1</F4></Su>
<Su pName="APMD_MIN"><F4>30.1</F4></Su>
<Su pName="APMD_MAX"><F4>30.1</F4></Su>
<Su pName="ARMD_MIN"><F4>30.1</F4></Su>
<Su pName="ARMD_MAX"><F4>30.1</F4></Su>
<Su pName="AXMD_MIN"><F4>30.1</F4></Su>
<Su pName="AXMD_MAX"><F4>30.1</F4></Su>
<Su pName="AYMD_MIN"><F4>30.1</F4></Su>
<Su pName="AYMD_MAX"><F4>30.1</F4></Su>
<Su pName="SXMN_MIN"><F4>30.1</F4></Su>
<Su pName="SXMN_MAX"><F4>30.1</F4></Su>
<Su pName="SYMN_MIN"><F4>30.1</F4></Su>
<Su pName="SYMN_MAX"><F4>30.1</F4></Su>
<Su pName="STMN_MIN"><F4>30.1</F4></Su>
<Su pName="STMN_MAX"><F4>30.1</F4></Su>
<Su pName="SXMD_MIN"><F4>30.1</F4></Su>
<Su pName="SXMD_MAX"><F4>30.1</F4></Su>
<Su pName="SYMD_MIN"><F4>30.1</F4></Su>
<Su pName="SYMD_MAX"><F4>30.1</F4></Su>
<Su pName="STMD_MIN"><F4>30.1</F4></Su>
<Su pName="STMD_MAX"><F4>30.1</F4></Su>
</DV>
</Data>
</EngineeingData>

thanks,
BRAHM
 
Back
Top