Can I generate a C# class from an XML file?
相关问题
- Sorting 3 numbers without branching [closed]
- Illegal to have multiple roots (start tag in epilo
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
I realise that this is a rather old post and you have probably moved on.
But I had the same problem as you so I decided to write my own program.
The problem with the "xml -> xsd -> classes" route for me was that it just generated a lump of code that was completely unmaintainable and I ended up turfing it.
It is in no way elegant but it did the job for me.
You can get it here: Please make suggestions if you like it.
SimpleXmlToCode
At first I thought the Paste Special was the holy grail! But then I tried it and my hair turned white just like the Indiana Jones movie.
But now I use http://xmltocsharp.azurewebsites.net/ and now I'm as young as ever.
Here's a segment of what it generated:
If you are working on .NET 4.5 project in VS 2012 (or newer), you can just Special Paste your XML file as classes.
EDIT > Paste Special > Paste XML As Classes
Use below syntax to create schema class from XSD file.
You can use xsd as suggested by Darin.
In addition to that it is recommended to edit the test.xsd-file to create a more reasonable schema.
type="xs:string"
can be changed totype="xs:int"
for integer valuesminOccurs="0"
can be changed tominOccurs="1"
where the field is requiredmaxOccurs="unbounded"
can be changed tomaxOccurs="1"
where only one item is allowedYou can create more advanced xsd-s if you want to validate your data further, but this will at least give you reasonable data types in the generated c#.
You should consider svcutil (svcutil question)
Both xsd.exe and svcutil operate on the XML schema file (.xsd). Your XML must conform to a schema file to be used by either of these two tools.
Note that various 3rd party tools also exist for this.