Generate C# class from XML

C#.NetXmlXsd

C# Problem Overview


Can I generate a C# class from an XML file?

C# Solutions


Solution 1 - C#

If you are working on .NET 4.5 project in VS 2012 (or newer), you can just Special Paste your XML file as classes.

  1. Copy your XML file's content to clipboard
  2. In editor, select place where you want your classes to be pasted
  3. From the menu, select EDIT > Paste Special > Paste XML As Classes

Solution 2 - C#

Yes, by using xsd.exe

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

Answer how to change directory in Developer Command Prompt to d:\temp may be useful.

If you generate classes for multi-dimensional array, there is a bug in XSD.exe generator, but there are workarounds.

Solution 3 - C#

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:

namespace Xml2CSharp
{
	[XmlRoot(ElementName="entry")]
	public class Entry {
		[XmlElement(ElementName="hybrisEntryID")]
		public string HybrisEntryID { get; set; }
		[XmlElement(ElementName="mapicsLineSequenceNumber")]
		public string MapicsLineSequenceNumber { get; set; }

Solution 4 - C#

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

Solution 5 - C#

You should consider svcutil ([svcutil question][1])

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.

[1]: https://stackoverflow.com/questions/1245896/is-svcutil-exe-a-replacement-for-xsd-exe "svcutil question"

Solution 6 - C#

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 to type="xs:int" for integer values
minOccurs="0" can be changed to minOccurs="1" where the field is required
maxOccurs="unbounded" can be changed to maxOccurs="1" where only one item is allowed

You 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#.

Solution 7 - C#

Use below syntax to create schema class from XSD file.

C:\xsd C:\Test\test-Schema.xsd /classes /language:cs /out:C:\Test\

Solution 8 - C#

To convert XML into a C# Class:

  • Navigate to the Microsoft Visual Studio Marketplace: -- https://marketplace.visualstudio.com
  • In the search bar enter text: -- xml to class code tool
  • Download, install, and use the app

Note: in the fullness of time, this app may be replaced, but chances are, there'll be another tool that does the same thing.

Solution 9 - C#

Found this site a bit ago. It converts XML and JSON to C# and Java classes. Has several options to tweak as you need. I use it pretty often. https://json2csharp.com/xml-to-csharp

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser496949View Question on Stackoverflow
Solution 1 - C#miszczuView Answer on Stackoverflow
Solution 2 - C#Darin DimitrovView Answer on Stackoverflow
Solution 3 - C#D. KermottView Answer on Stackoverflow
Solution 4 - C#TalonView Answer on Stackoverflow
Solution 5 - C#ng5000View Answer on Stackoverflow
Solution 6 - C#Albin SunnanboView Answer on Stackoverflow
Solution 7 - C#Kiran.BakwadView Answer on Stackoverflow
Solution 8 - C#J WoodView Answer on Stackoverflow
Solution 9 - C#Jeremy HodgeView Answer on Stackoverflow