expression

Serialize and escape XML text

Use the serializer for XML text rather than concatenating markup with user values. A round trip checks the data rather than the exact spelling of escapes. This example uses external crates; see the staged Cargo manifest. Expected stdout (not executed): A & B < C Common mistake: Building XML by direct string interpolation of arbitrary text.

Formal Syntax & Structure

rust syntaxrust
quick_xml::se::to_string(&value)

Working Example

Example Snippetrust
fn main() {    #[derive(serde::Serialize, serde::Deserialize)]    #[serde(rename = "note")]    struct Note { body: String }    let note = Note { body: "A & B < C".into() };    let xml = quick_xml::se::to_string(&note).unwrap();    let back: Note = quick_xml::de::from_str(&xml).unwrap();    println!("{}", back.body);}
Rust Reference