Function quick_xml::se::to_writer

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

Serialize struct into a Writer.

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>"
);