XML to C# Class Generator
CV-78Paste an XML document and press Convert to get classes with properties and JSON attributes. The XML is read into JSON first, so attributes, text and repeated elements follow the rules below, and it runs in your browser.
Runs in your browser. Nothing is uploaded.
XML
C#
How to use it
- Paste an XML document, or open an .xml file. Use one that shows every element you expect, with repeating elements repeated.
- Pick System.Text.Json or Newtonsoft.Json for the attributes, and turn nullable reference types on or off. Leave typed values on to read 59.90 as a number and true as a boolean, or turn it off to keep every value as text.
- Press Convert, then copy the result or download it as a .cs file.
- Deserialize with JsonSerializer.Deserialize<Root>(json) for System.Text.Json or JsonConvert.DeserializeObject<Root>(json) for Newtonsoft.Json. Feed it the JSON form of your XML, not the XML itself.
How the classes are built
Each object becomes a public class with { get; set; } properties written in PascalCase. Whole numbers become int, or long once they pass the 32-bit range, and decimals become double. Arrays become List<T>, with the item type worked out from every item rather than only the first. A key that is null or missing in some items gets a nullable type such as int?, and string? when nullable reference types are on; mixed types and empty arrays become object.
How the XML becomes classes
The XML is read into JSON first, using the same rules as the XML to JSON converter, and the code is built from that JSON. The root element becomes the single top-level key, so the first type holds one member named after it. An attribute becomes a key that starts with @, and text that sits next to attributes or child elements becomes #text. In C#, @id becomes a property named Id with the original key in the attribute, #text becomes Text, and a clash with a child element of the same name gets a number.
What a sample cannot tell it
The result only reflects the sample you paste: a key that never appears has no member, and a number that is always whole in the sample is typed as whole even if other data holds decimals. Dates stay string; change them to DateTime or DateTimeOffset by hand. An element that appears once in your sample is written as a single value; if real documents can repeat it, paste a sample where it appears at least twice so it becomes a list.
Questions people ask
How do I convert XML to a C# class?
Paste the XML into the input box, choose your options and press Convert to get the classes. Deserialize with JsonSerializer.Deserialize<Root>(json) for System.Text.Json or JsonConvert.DeserializeObject<Root>(json) for Newtonsoft.Json. The classes follow the JSON form of the XML. The conversion happens in your browser.
What does the nullable reference types option change in the C# classes?
With it on, the file starts with #nullable enable, reference properties that can be null get a ? such as string?, and other reference properties get = default!; so the compiler does not warn about uninitialized properties. Value types such as int become int? either way when a key can be null. This applies to the JSON form of your XML.
Why is a large number written as double in C#?
A whole number beyond the range of long has no exact simple C# integer type, so it becomes double, which can round it, and the line gets a comment. If you need every digit, change the property to decimal or string by hand. This applies to the JSON form of your XML.
Does it turn date text into DateTime in C#?
No. Strings stay string, even when they look like an ISO date or a GUID. Change those properties to DateTime, DateTimeOffset or Guid yourself if you want typed values; the rest of the class stays as generated. This applies to the JSON form of your XML.
What happens to XML attributes in the generated code?
Each attribute becomes a key that starts with @, such as @id, so it is a member of the generated type next to the child elements. The member name drops the @, and the original key is kept in the code where the language allows it. The C# classes are written from that converted data.
Why is an element a list in one XML sample and a single value in another?
XML does not say how many times an element may repeat. An element becomes a list only when it appears more than once under the same parent in your sample. To get a list type, paste a sample where the element appears at least twice. The C# classes are written from that converted data.
About this tool
The XML is read first and turned into the same JSON that the XML to JSON converter writes, and the code is generated from that. The root element becomes the single top-level key, so the root class holds one field named after it. An attribute becomes a key that starts with @, text next to attributes or child elements becomes #text, an element that appears more than once under one parent becomes a list, and one that appears once stays a single value, so a sample with one item gives a single object where another sample would give a list. Comments, processing instructions and the DOCTYPE are dropped. With the typed-values option on (the default), text that is exactly a number or true/false becomes a number or boolean; text with a leading zero or plus sign stays a string, and turning the option off makes every value a string. The result describes the converted JSON, not an XmlSerializer or JAXB mapping. Every object becomes a class with { get; set; } properties in PascalCase, and an attribute keeps the original key whenever the property name differs ([JsonPropertyName] for System.Text.Json or [JsonProperty] for Newtonsoft.Json). Whole numbers become int, or long beyond 32 bits; decimals become double. A whole number beyond long also becomes double and can round, and a comment marks the line. Arrays become List<T>. A key that is null or missing in some items gets a nullable type, and mixed types, always-null keys and empty arrays become object. It does not add validation attributes or date types. Everything runs in your browser; nothing is uploaded.