XML to C# Class Generator
Classes with XmlSerializer attributes, ready to deserialize
XML Input
C# Output
Paste your XML on the left and the C# code shows up here.
Or drop a file on this box, , or .
What the generator writes
Paste an XML sample and you get C# classes annotated for XmlSerializer. The root class gets [XmlRoot("order")] with the real root element name, attributes get [XmlAttribute], child elements [XmlElement], and text next to attributes [XmlText].
Repeated elements become List<T> initialised to an empty list, which is how XmlSerializer collects unwrapped repeated elements. Numbers are typed int, long, double or bool from the values you paste. With Nullable for missing fields on, the file starts with #nullable enable, missing elements become int? or string?, and required strings default to string.Empty. The attribute reference explains each annotation.
We compiled the output with .NET 8 and deserialized the page sample with it. Number and boolean attributes stay non-nullable on purpose, because XmlSerializer refuses Nullable<T> on [XmlAttribute]. The XML is processed in your browser and not uploaded.
How to use it
Paste, pick two options, copy.
Paste a sample
Paste XML into XML Input or load one with Upload. The two <item> children under <order> are what produce List<Item>; with a single one you would get a plain Item property. Sample loads an order:
<order id="ORD-7731" status="paid">
<item sku="SKU-101"><qty>2</qty><price currency="USD">24.99</price></item>
<item sku="SKU-205"><qty>1</qty><price currency="USD">39.50</price></item>
</order> A typo in the markup puts the DOMParser message in the C# pane instead of classes. The XML Validator narrows it down to the exact position.
Choose properties and nullability
Root class renames the outer class only; [XmlRoot("order")] keeps the real element name so deserialising still works. Properties toggles { get; set; } auto-properties versus public fields. Nullable for missing fields adds #nullable enable, marks class-typed members like Customer? and gives strings a string.Empty default, in line with nullable reference types.
Deserialize
Download the .cs file, then var order = (Order)new XmlSerializer(typeof(Order)).Deserialize(stream);.
Where it helps
SOAP and legacy services
When you have a sample message but no WSDL or XSD, generated classes get you deserializing in minutes.
ASP.NET Core XML endpoints
With AddXmlSerializerFormatters, annotated classes like these work as request and response models.
Config and import files
Read vendor XML exports into typed objects in a background service or console tool.
Unity and desktop apps
XmlSerializer is available in Unity and WPF, so the same classes work for save files and settings.
Common questions
Will XmlSerializer accept these classes as they are?
Yes. Classes are public with public members and an implicit parameterless constructor, which is what XmlSerializer needs. [XmlRoot] uses the actual root element name, so the root does not have to be called RootObject.
How are attributes and text mapped?
Each attribute gets [XmlAttribute("sku")]-style markup and each child [XmlElement]. An element with an attribute plus text, say <weight unit="kg">1.2</weight>, becomes a Weight class whose text sits in a Value property marked [XmlText]. If a child would share its class's name, the member gets a Value suffix to avoid compiler error CS0542.
How are namespaces handled?
Elements and attributes in a namespace get a Namespace = "..." argument with the URI from your sample, and the root gets it on [XmlRoot]. A child with no namespace inside a namespaced element gets Namespace = "", since XmlSerializer would otherwise look for it in the parent's namespace.
How are types chosen?
From the values you paste: qty becomes int, an integer too big for 32 bits long, a decimal like 39.50 double, and true/false bool. Leading-zero values stay string, so does a value left empty anywhere in the sample (XmlSerializer cannot read an empty int), and so do timestamps like placedAt; switch to DateTime, or to decimal for money, by hand.
Why is a member called CategoryValue?
C# does not allow a member with the same name as its class, so when an element contains a child of the same name, the member gets a Value suffix. The XML name in the attribute is unchanged.
Is the XML uploaded?
No. The classes come out of JavaScript running in this tab, with nothing posted to a server. The editor stops at 256 KB, and one order with a couple of items shows the generator every member it has to write.
Related tools
Other converters you might reach for next: