简体   繁体   中英

how to create c# class from Xsd

               <?xml version="1.0" encoding="utf-8"?>
<domain ID="ui0" xmlns="http://tempuri.org/test.xsd">
  <info>
    <title>title1</title>
    <description>description1</description>
    <units>
      <unit>unittype1</unit>
      <value>value1</value>
    </units>
 </info>
 <freeman>
    <shows>
      <show id="id1">
        <Type>Type1</Type>
        <Name>Name1</Name>
        <classes>
          <interface id="id1">
            <id>ipMask1</id>
            <traffic>traffic1</traffic>
            <description>description1</description>
          </interface>
        </classes>
      </show>
   </shows>
    <links>
      <link id="id1">
        <from>fromNode1</from>
        <to>toNode1</to>
      </link>
    </links>
  </freeman>
 </domain>

How to create c# class for this, i have created a class but while generationg xml the order is missing? please help me on this, how to add data to classes

On Windows, there's a Microsoft program called xsd.exe that will turn your XML into a schema (XSD), and generate C# classes from that. It's installed with Visual Studio on Windows. It's in C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools or similar, although if you start a Visual Studio Developer Command Prompt it will be on the path. To use it with your file:

  • Save the XML data as data.xml or similar (it's XML not an XSD clearly)
  • The tool doesn't like the fact that 'interface' has both an attribute called 'id' and an element called 'id', so rename one of them or take one out.
  • To generate an XSD file called data.xsd: start a Developer Command prompt then navigate to the folder with data.xml in and do:

    xsd data.xml

  • To generate C# classes in a file called data.cs do:

    xsd /c data.xsd

The resulting classes are serializable, so you can deserialize the XML into them.

The drawback of doing it this way is the classes you get are complex (I'm getting 340 lines of code with your XML,). so crafting your classes by hand may be better.

I wrote about this (a long long time ago) with respect to the very complex FpML schemas.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM