Open XML file without special characters

Viewed 21

How can we open an XML file without special characters added at parsing time?

The following code open an xml file and try to deserialize it but when the file is opened many add special characters has been added :

#[macro_use]
extern crate serde_derive;

extern crate serde;
extern crate serde_xml_rs;

#[derive(Debug, Deserialize)]
struct Item {
    pub name: String,
    pub source: String,
}

#[derive(Debug, Deserialize)]
struct Project {
    pub name: String,
    #[serde(rename = "Item", default)]
    pub items: Vec<Item>,
}

fn main() {
   read_file();
}

fn read_file() -> std::io::Result<()>  {
    let file = File::open("C:\\XXX\\xxx\\project.xml")?;
    let mut buf_reader = BufReader::new(file);
    let mut contents = String::new();
    buf_reader.read_to_string(&mut contents)?;
    println!("Content file : {:?}", "r##".to_owned() + &contents + &"##".to_owned());

    let prj: Project = serde_xml_rs::deserialize( ("r##".to_owned() + &contents + &"##".to_owned()).as_bytes()).unwrap();

    println!("{:?}", prj);

    Ok(())
}

`The Println methode return this :

Content file :

"r##<Project name=\"my_project\">\r\n    <Item>\r\n\t\t<name>Test</name>\r\n\t\t<source>Source</Source>\r\n\t</Item>\r\n</Project>##"

whereas the file is like this :

<Project name="my_project">
    <Item>
        <name>Test</name>
        <source>Source</Source>
    </Item>
</Project>

But the deserialize function works wihtout any special characters. But backslashes and \r\n\t\t have been added during the open file.

how to solve this issue ?

0 Answers
Related