XML to F# Converter - Generate F# Record Types from XML
Turn an XML document into F# record types with a FromXml member that reads it with System.Xml.Linq.
How to Convert XML to F# Record Types
Paste or Upload Your XML
Paste XML into the left editor, upload an .xml or .txt file, or press Sample to load a worked example.
Example XML
<?xml version="1.0" encoding="UTF-8"?>
<financial-report id="FIN-2024-Q1" period="quarterly">
<company>
<name>TechCorp Solutions</name>
<founded>2010</founded>
<is-public>true</is-public>
</company>
<financials>
<revenue currency="USD">15750000.50</revenue>
<growth-rate>12.5</growth-rate>
</financials>
</financial-report>Let It Read the Structure
The F# output updates as you type. Every element and attribute is looked at, including all copies of repeated elements. Broken XML shows the line and column of the first problem instead of code.
Check the Generated Records
Look over the record types, field names and types. The original XML names are kept in FromXml, so the F# names can follow F# conventions.
F# output for the example
The file also starts with a few comment lines, the module line, the opens and the XmlRead helper module; the records follow:
type Root =
{ Id: string option // attribute id
Period: string option // attribute period
Company: Company option
Financials: Financials option }
static member FromXml(node: XElement) : Root =
{ Root.Id = XmlRead.attr node "id" ""
Root.Period = XmlRead.attr node "period" ""
Root.Company = XmlRead.children node "company" "" |> List.tryHead |> Option.map Company.FromXml
Root.Financials = XmlRead.children node "financials" "" |> List.tryHead |> Option.map Financials.FromXml }
and Company =
{ Name: string option
Founded: int option
IsPublic: bool option }
static member FromXml(node: XElement) : Company =
{ Company.Name = XmlRead.children node "name" "" |> List.tryHead |> Option.map XmlRead.text
Company.Founded = XmlRead.children node "founded" "" |> List.tryHead |> Option.map (XmlRead.text >> XmlRead.toInt)
Company.IsPublic = XmlRead.children node "is-public" "" |> List.tryHead |> Option.map (XmlRead.text >> XmlRead.toBool) }
and Financials =
{ Revenue: Revenue option
GrowthRate: float option }
static member FromXml(node: XElement) : Financials =
{ Financials.Revenue = XmlRead.children node "revenue" "" |> List.tryHead |> Option.map Revenue.FromXml
Financials.GrowthRate = XmlRead.children node "growth-rate" "" |> List.tryHead |> Option.map (XmlRead.text >> XmlRead.toFloat) }
and Revenue =
{ Currency: string option // attribute currency
Value: float option // text content
}
static member FromXml(node: XElement) : Revenue =
{ Revenue.Currency = XmlRead.attr node "currency" ""
Revenue.Value = XmlRead.ownText node |> Option.map XmlRead.toFloat }Copy or Download the Code
Copy the code or download it as XmlData.fs, add it to your project, and load documents with Root.FromXml.
What is XML to F# Conversion?
It means writing F# types that match the shape of an XML document, so you can pattern match on typed fields instead of walking XElement nodes by hand. That gets tedious once a document has a few levels of nesting and some repeated elements.
This converter looks at the whole document, merges every copy of each element, and writes a record for each element that has children or attributes. Each record gets a FromXml member that reads the document with System.Xml.Linq and parses numbers with the invariant culture. If you would rather have types inferred at compile time, the XmlProvider in FSharp.Data is the other common route.
Frequently Asked Questions
What does the generated F# code contain?
One record type for each XML element that has child elements or attributes, written as a single type ... and ... group so the order of the records never matters. Every record has a static FromXml member that fills it from an XElement, and a small XmlRead module at the top does the System.Xml.Linq lookups and the number parsing. Fields are option values, and elements that repeat under one parent become a list, so a document that leaves something out still loads.
How are the field types chosen?
From the text of every value the element has in your document. Whole numbers that fit 32 bits become int, bigger ones become int64 (so an ID like 8901240544102066246 keeps every digit), and positive values past that up to 18446744073709551615 become uint64. Numbers with a decimal point become float, and only the exact words true and false become bool. Values with leading zeros such as 00501, hex like 0x1F, anything past 64 bits and elements that are sometimes empty stay string, because parsing them would change or lose data.
How do I read an XML document with these records?
Add the downloaded XmlData.fs to your project and call Root.FromXml(XDocument.Parse(xmlText).Root), or XDocument.Load(path).Root for a file. It only needs System.Xml.Linq, which is part of .NET, so there is no NuGet package to install. Numbers are parsed with the invariant culture, so 14.50 reads the same on a German or French machine. The generated code was checked by loading it into F# on .NET 8 and reading each test document back.
What happens with repeated elements and different copies of the same element?
All copies of an element name are merged before any code is written. A field that only appears in the second <metric> still makes it into the record, and if a value is a number in one copy and text in another, the field becomes a string. When an element appears more than once under the same parent, its field becomes a list. An element with both attributes and text gets a record with the attributes plus a Value field for the text.
How are XML names turned into F# names?
Record and field names use PascalCase, so is-public becomes IsPublic and service-info becomes ServiceInfo. Names starting with a digit get a Field or Element prefix, names that would clash with the record's own members (ToString, Equals, FromXml) get a Value suffix, type names such as String or List get an Element suffix, and duplicates get a number. The original XML name stays in the FromXml code, including its namespace, so lookups still match the document.
Is my XML uploaded anywhere?
No. The conversion runs in your browser, and the XML you paste or upload is not sent to our server.