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

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

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