XML to C#

Generate C# classes with XmlSerializer attributes from an XML sample.

XML to C# – Classes for XmlSerializer

The XML to C# converter creates C# classes that System.Xml.Serialization.XmlSerializer can read and write without extra configuration. It is the browser-based alternative to Visual Studio's Paste Special → Paste XML as Classes, but produces cleaner, modern code: nullable reference types, auto-properties and List<T> instead of arrays.

How XML maps to types

XML has no native types, so the converter reads every element and attribute value and infers one: whole numbers become integers, decimals become floating-point numbers, true/false become booleans, and everything else stays a string. Attributes are kept apart from child elements (they appear as @name keys internally), an element that has both attributes and text gets a dedicated text field, and repeated sibling elements such as several item nodes become a list. Namespace prefixes are stripped from names so ns:customer and customer produce the same type.

Attributes in the output

  • The root class is decorated with [XmlRoot("order")].
  • Child elements get [XmlElement("name")], preserving the exact casing of the XML while the property uses PascalCase.
  • XML attributes become properties with [XmlAttribute("name")].
  • Mixed elements with attributes and text get a Value-style property with [XmlText].
  • Repeated elements become List<T> with [XmlElement], which XmlSerializer reads as an unwrapped list.

Deserialise with new XmlSerializer(typeof(Order)).Deserialize(reader). You can switch the attribute style to System.Text.Json or Newtonsoft if you are converting an XML sample into JSON DTOs instead.

Good to know

XmlSerializer needs public parameterless constructors, so keep the default class output rather than records when you serialise XML. Numeric attributes cannot be nullable with XmlSerializer; if an attribute is optional, change it to string or add a ShouldSerialize pattern.

Validate your document with the XML Validator or generate C# from JSON with JSON to C#.

Frequently Asked Questions

No. The XML is parsed and converted by JavaScript running in your browser tab. Nothing is sent to a server, so internal feeds, SOAP payloads and configuration files stay on your machine.

Yes. XmlSerializer and the attributes used are available in .NET Framework 2.0+ and every .NET Core / .NET version. Nullable annotations (string?) need C# 8 or later; remove the ? marks for older compilers.

Visual Studio generates classes from an inferred XSD with arrays, partial classes and verbose attributes. This tool produces compact auto-properties with List<T>, and you can run it anywhere without Visual Studio.

Yes. Set the Namespace option, or clear it to emit the classes without a namespace block.