Loading Rust to XML Converter...

How to Convert a Rust Struct to XML

Before you wire quick-xml into a project, it helps to see what your structs will actually look like on the wire. Paste a value from a test, a fixture or a log line and you get the XML without building anything.

Step 1

Paste a Rust value

Paste a struct value such as Invoice { id: 1, total: 9.99, ... }, a vec![...] of them, or a whole let statement. You can also upload a .rs file or click Sample. If the file also holds struct definitions and a fn main, the value of the first let statement is used.

Invoice {
    id: 8901240544102066246,
    customer: Customer { name: "Ann & Co".to_string(), zip: "00501".to_string() },
    total: 14.50,
    lines: vec![
        Line { sku: "A-1".into(), qty: 2 },
        Line { sku: "B-7".into(), qty: 1 },
    ],
    note: None,
}
Step 2

Read the XML

The XML shows up on the right a moment after you stop typing. The struct name becomes the root element, every field becomes a child element, nested structs nest, and each item of a Vec repeats the field element. Anything that is not plain data, like a variable or a macro call, gets an error with the line and column.

The example above comes out like this. The ampersand is escaped, the two lines repeat the lines element, None is an empty element, and the long ID, the zip code and the zero in 14.50 are untouched:

<?xml version="1.0" encoding="UTF-8"?>
<Invoice>
  <id>8901240544102066246</id>
  <customer>
    <name>Ann &amp; Co</name>
    <zip>00501</zip>
  </customer>
  <total>14.50</total>
  <lines>
    <sku>A-1</sku>
    <qty>2</qty>
  </lines>
  <lines>
    <sku>B-7</sku>
    <qty>1</qty>
  </lines>
  <note/>
</Invoice>
Step 3

Copy or download it

Copy puts the XML on your clipboard, and Download saves it as data.xml in UTF-8. Notes above the output tell you when something was written differently from what quick-xml would print, for example a decimal kept as typed.

What is Rust to XML Conversion?

XML is still what many SOAP services, payment gateways, feeds and older config files expect. In Rust you usually derive Serialize with Serde and hand the value to quick_xml::se::to_string, which turns each field into an element.

This page shows that result for a value you already have, so you can check element names and nesting before you write the code. Once the XML looks right, you can tidy it with the XML Formatter, check it with the XML Validator, or go the other way with XML to Rust, which writes structs for an XML file. The same values can also become JSON or YAML.

Frequently Asked Questions

What does the XML look like?

It follows the layout that quick-xml uses when it serializes a struct with serde: the struct name is the root element, each field becomes an element named after the field, a nested struct becomes a nested element, and a Vec repeats the field element once per item, with no wrapper around the items. None and empty strings become empty elements like <note/>, and empty lists write nothing. The output is indented with two spaces and starts with an XML declaration.

Is it really the same as quick-xml?

For ordinary structs, yes. We ran our test values through quick-xml 0.37 with serde and its indented output matched this page line for line. A few things differ on purpose: decimals stay exactly as you typed them (quick-xml prints the f64, so 14.50 becomes 14.5), a carriage return is written as &#13; so XML readers keep it, and control characters that XML 1.0 cannot hold become U+FFFD. Field attributes such as #[serde(rename = "...")] live on the struct definition, not the value, so they are not applied here.

What happens with a vec! of structs?

quick-xml refuses to write a list on its own, because an XML document needs exactly one root element. This page wraps the items in a root named after them, so vec![User { ... }, User { ... }] becomes <UserList> with a <User> element per item. In Rust you get the same result from a struct like UserList with #[serde(rename = "User")] on a Vec<User> field. A single plain value is wrapped in <root>.

Will my numbers and text change?

Numbers are copied as text and never turned into JavaScript numbers, so 8901240544102066246, -9223372036854775808 and u128::MAX keep every digit, and a quoted "00501" keeps its leading zeros. Underscores and type suffixes are dropped (1_000u32 becomes 1000) and hex, octal and binary literals are written as decimal, which is the value Rust stores. In text, &, < and > are escaped, and Rust escapes like \n, \" and \u{1F600} are decoded the way rustc reads them.

Which Rust syntax can I paste?

Struct literals with any path (models::User { ... }), vec![...] and arrays, tuples and tuple structs, unit enum values like Status::Active (written as Active), Some(...) and None, true and false, chars, every string form including raw strings r#"..."#, and the usual .to_string(), .into() and String::from(...) wrappers. Variables, function calls that compute something, and macros other than vec! cannot be run here, so they get an error that points at them. HashMap literals are not read yet.

Is my code uploaded anywhere?

No. The conversion runs in your browser, the text is not sent to our server, and nothing is kept: a reload starts with an empty editor. Uploaded .rs files are read in the browser too, up to 10 MB.