quick_xml::se

Function to_writer_with_root

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

Serialize struct into a Writer using specified root tag name. root_tag should be valid XML name, otherwise error is returned.

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_with_root(&mut buffer, "top-level", &data).unwrap();
assert_eq!(
    buffer,
    "<top-level attribute=\"attribute content\">\
        <element>element content</element>\
        text content\
    </top-level>"
);