quick_xml::se

Function to_writer

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

Serialize struct into a Writer.

Returns the classification of the last written type.

ยง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",
};

let mut buffer = String::new();
to_writer(&mut buffer, &data).unwrap();
assert_eq!(
    buffer,
    // 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>"
);