Loading XML to C# Converter...
Please wait a moment

How to Convert XML to C# Classes - Step by Step Guide

Step 1

Paste or Upload Your XML

Start with the XML you want to read in C#: a config file, an API response or a sample message. One representative document is enough, but the more copies of a repeated element it has, the better the types get.

Example: Try This XML Data

Copy and paste this XML example to see how it works:

<?xml version="1.0" encoding="UTF-8"?>
<order>
  <customer id="C001">
    <name>Isabella Thompson</name>
    <email>[email protected]</email>
    <phone>+1-555-0123</phone>
    <isPremium>true</isPremium>
  </customer>
  <orderDetails>
    <orderId>ORD-2024-001</orderId>
    <orderDate>2024-01-15</orderDate>
    <totalAmount>299.99</totalAmount>
    <currency>USD</currency>
  </orderDetails>
</order>
Step 2

Let It Read the Structure

There is nothing to set up. The output follows the usual XmlSerializer conventions and updates as you type:

•
Serialization attributes: every property carries the original XML name in an [XmlElement], [XmlAttribute] or [XmlText] attribute
•
Nested classes: each element with children or attributes gets its own class; repeats become List<T>
•
Types from the values: int, long, ulong, double and bool only when every value fits; everything else is string
•
Clear errors: broken XML shows the line and column of the first problem
Step 3

Check the Generated Classes

This is exactly what the example above turns into. Note that orderDate stays a string (it is a date, not a number) and isPremium becomes a bool.

C# Code Output

Your XML becomes these C# classes:

using System.Collections.Generic;
using System.Xml.Serialization;

// Read a document with: var data = (Root)new XmlSerializer(typeof(Root)).Deserialize(reader);
namespace XmlToCSharp
{
    [XmlRoot("order")]
    public class Root
    {
        [XmlElement("customer")]
        public Customer Customer { get; set; }

        [XmlElement("orderDetails")]
        public OrderDetails OrderDetails { get; set; }
    }

    public class Customer
    {
        [XmlAttribute("id")]
        public string Id { get; set; }

        [XmlElement("name")]
        public string Name { get; set; }

        [XmlElement("email")]
        public string Email { get; set; }

        [XmlElement("phone")]
        public string Phone { get; set; }

        [XmlElement("isPremium")]
        public bool IsPremium { get; set; }
    }

    public class OrderDetails
    {
        [XmlElement("orderId")]
        public string OrderId { get; set; }

        [XmlElement("orderDate")]
        public string OrderDate { get; set; }

        [XmlElement("totalAmount")]
        public double TotalAmount { get; set; }

        [XmlElement("currency")]
        public string Currency { get; set; }
    }
}
Step 4

Copy or Download the Code

Copy the code or download it as XmlClasses.cs, add it to your project and read documents with XmlSerializer.

•
Copy to clipboard and paste into an existing file
•
Download XmlClasses.cs and add it to your project
•
Rename freely: change the namespace or the Root class name; the XML names live in the attributes
•
Use in ASP.NET Core through the XML serializer formatters

What is XML to C# Conversion?

XML to C# conversion means writing the C# classes that an XML document maps onto, so .NET can read the document into typed objects instead of walking XmlDocument nodes by hand. Doing that by hand gets tedious fast for anything with more than a few elements.

The classes here are meant for XmlSerializer: the attributes keep the exact XML names, so the C# side can use normal PascalCase names and still match your documents, and the same classes can write the XML back out.

Frequently Asked Questions

What does the generated C# code look like?

Plain classes with auto-properties and the attributes from System.Xml.Serialization: [XmlRoot] on the top class, [XmlElement] and [XmlAttribute] on properties, and [XmlText] where an element has attributes and a text value. The top class is called Root and carries the real root element name. Nested elements get their own classes and repeated elements become List<T>. Read a file with new XmlSerializer(typeof(Root)).Deserialize(reader).

How are the property types chosen?

From every value the element or attribute has anywhere in the document. Whole numbers become int, or long once they no longer fit in 32 bits, so an ID like 8901240544102066246 is a long and keeps every digit; ulong covers larger unsigned values and anything past 64 bits stays a string. Decimals become double, which means 14.50 is read as 14.5. Only lowercase true and false become bool. Values like 00501, hex such as 0x1F, and elements that are sometimes empty stay string, because XmlSerializer would drop the zeros or throw on an empty int.

What happens with repeated elements and attributes?

Every copy of a repeated element is merged into one class, so a child that only shows up in the third <subscriber> is still a property, and a value that is a number in one copy and text in another becomes a string. Attributes become [XmlAttribute] properties. An element like <price currency="USD">10.50</price> gets its own class with a Currency attribute and a Value property for the text.

Does it handle XML namespaces?

Yes. When the document uses namespaces, each [XmlRoot], [XmlElement] and [XmlAttribute] states its namespace with Namespace = "...", so prefixed elements like <ns:item> and default xmlns documents read back correctly. The xmlns declarations themselves and xsi:nil or xsi:type attributes are left out, since XmlSerializer deals with those on its own.

What if my element names are not valid C# names?

Names are turned into PascalCase, so customer-id becomes CustomerId, and the original XML name stays in the attribute. A name starting with a digit gets an X in front, two names that end up the same get a number (FooBar, FooBar2), and a property is renamed if it would share its class name, which C# does not allow. Elements that would produce a class named after a common System type, such as <string>, get an Element suffix.

Can I use these classes in ASP.NET Core or with SOAP services?

They are ordinary XmlSerializer classes, so anything built on XmlSerializer can use them, including ASP.NET Core once XML support is switched on with AddXmlSerializerFormatters. For a full SOAP or WCF client, generating the contract from the service WSDL with dotnet-svcutil is the better route; these classes are handy when you only need to read or write one kind of message.

Is my XML uploaded anywhere?

No. The conversion runs in your browser and the XML never leaves your machine, so the only practical limit on file size is your browser's memory.