Problem
Is it possible to create a C# class from an XML file?
Asked by user496949
Solution #1
You can Special Paste your XML file as classes in VS 2012 (or newer) if you’re working on a.NET 4.5 project.
Answered by miszczu
Solution #2
Yes, xsd.exe can be used.
D:\temp>xsd test.xml
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 4.0.30319.1]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'D:\temp\test.xsd'.
D:\temp>xsd test.xsd /classes
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 4.0.30319.1]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'D:\temp\test.cs'.
Notes
It can be useful to know how to change directory in Developer Command Prompt to d:temp.
There is an issue in the XSD.exe generator when generating classes for multi-dimensional arrays, although there are workarounds.
Answered by Darin Dimitrov
Solution #3
I thought the Paste Special was the holy grail at first! However, when I tried it, my hair went white, much as in the Indiana Jones film.
But now that I’m using http://xmltocsharp.azurewebsites.net/, I’m as young as I’ve ever been.
Here’s a sample of the output:
namespace Xml2CSharp
{
[XmlRoot(ElementName="entry")]
public class Entry {
[XmlElement(ElementName="hybrisEntryID")]
public string HybrisEntryID { get; set; }
[XmlElement(ElementName="mapicsLineSequenceNumber")]
public string MapicsLineSequenceNumber { get; set; }
Answered by D. Kermott
Solution #4
I realize this is an old post, and you have most likely moved on.
But, because I was having the same issue as you, I decided to develop my own program.
For me, the “xml -> xsd -> classes” path resulted in a blob of code that was utterly unmaintainable, so I ditched it.
It’s not really elegant, but it got the job done for me.
It’s available here: If you like it, please give suggestions.
SimpleXmlToCode
Answered by Talon
Solution #5
svcutil is a good option (svcutil question)
The XML schema file is used by both xsd.exe and svcutil (.xsd). To be used by either of these tools, your XML must comply to a schema file.
It’s worth noting that there are a variety of third-party tools available for this.
Answered by ng5000
Post is based on https://stackoverflow.com/questions/4203540/generate-c-sharp-class-from-xml