Skip to main content

to_string

Function to_string 

Source
pub fn to_string<T>(value: &T) -> Result<String>
where T: Serialize + ?Sized,
Expand description

Serializes a value to an XML string.

§Maps

Map keys are used verbatim as element names, so they must be valid XML names for the output to be strictly valid XML. Keys such as 1 or strings containing spaces produce structurally balanced but not strictly valid documents. Keys containing markup characters (<, >, &, quotes, or whitespace) are written verbatim into the tag names and will break the document structure — map keys must be trusted or validated by the caller.

§Example

use serde::Serialize;
use serde_xml::to_string;

#[derive(Serialize)]
struct Person {
    name: String,
    age: u32,
}

let person = Person {
    name: "Alice".to_string(),
    age: 30,
};

let xml = to_string(&person).unwrap();
assert!(xml.contains("<name>Alice</name>"));