quick_xml::se

Function to_string

Source
pub fn to_string<T>(value: &T) -> Result<String, SeError>
where T: ?Sized + Serialize,
Available on crate feature serialize only.
Expand description

Serialize struct into a String.

ยงExamples

#[derive(Serialize)]
struct Root<'a> {
    #[serde(rename = "@attribute")]
    attribute: &'a str,
    element: &'a str,
    #[serde(rename = "$text")]
    text: &'a str,
}

let data = Root {
    attribute: "attribute content",
    element: "element content",
    text: "text content",
};

assert_eq!(
    to_string(&data).unwrap(),
    // The root tag name is automatically deduced from the struct name
    // This will not work for other types or struct with #[serde(flatten)] fields
    "<Root attribute=\"attribute content\">\
        <element>element content</element>\
        text content\
    </Root>"
);