英文:
serde-xml-rs: LastElementNameNotAvailable when serializing
问题
在我的项目中,我遇到了一个错误,其中我使用 serde-xml-rs
解析和序列化 XML。
Cargo.toml
[package]
name = "serde_xml_rs_serialization_error"
version = "0.1.0"
edition = "2021"
# 更多键及其定义,请参见 https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = { version = "1.0.164", features = ["derive"] }
serde-xml-rs = "0.6.0"
src/main.rs
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Deserialize, Serialize)]
struct Types {
#[serde(rename = "type")]
types: Vec<Type>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
struct Type {
name: String,
}
const XML: &str = r#"<types>
<type name="first"/>
<type name="second"/>
</types>"#;
fn main() {
let types: Types = serde_xml_rs::from_str(XML).expect("无法解析 XML。");
dbg!(&types);
match serde_xml_rs::to_string(&types) {
Ok(xml) => {
dbg!(xml);
}
Err(error) => {
dbg!(error);
}
};
}
我期望得到一个序列化的 XML 字符串,但实际上它产生了一个错误:
cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.05s
Running `target\debug\serde_xml_rs_serialization_error.exe`
[src\main.rs:21] &types = Types {
types: [
Type {
name: "first",
},
Type {
name: "second",
},
],
}
[src\main.rs:27] error = Writer {
source: LastElementNameNotAvailable,
}
我在这里做错了什么?
1: https://docs.rs/serde-xml-rs/latest/serde_xml_rs/
英文:
So I encountered this error in one of my projects where I parse and serialize XML using serde-xml-rs
.
Cargo.toml
[package]
name = "serde_xml_rs_serialization_error"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = { version = "1.0.164", features = ["derive"] }
serde-xml-rs = "0.6.0"
src/main.rs
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Deserialize, Serialize)]
struct Types {
#[serde(rename = "type")]
types: Vec<Type>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
struct Type {
name: String,
}
const XML: &str = r#"<types>
<type name="first"/>
<type name="second"/>
</types>"#;
fn main() {
let types: Types = serde_xml_rs::from_str(XML).expect("Cannot parse XML.");
dbg!(&types);
match serde_xml_rs::to_string(&types) {
Ok(xml) => {
dbg!(xml);
}
Err(error) => {
dbg!(error);
}
};
}
I expected a serialized XML string, but instead it produces an error:
cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.05s
Running `target\debug\serde_xml_rs_serialization_error.exe`
[src\main.rs:21] &types = Types {
types: [
Type {
name: "first",
},
Type {
name: "second",
},
],
}
[src\main.rs:27] error = Writer {
source: LastElementNameNotAvailable,
}
What am I doing wrong here?
答案1
得分: 0
这是一个关于serde-xml-rs
的问题,让我浪费了半天的时间。
使用 quick-xml
替代 serde-xml-rs
解决了这个问题:
Cargo.toml
[package]
name = "serde_xml_rs_serialization_error"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = { version = "1.0.164", features = ["derive"] }
quick-xml = { version = "0.28.2", features = ["serialize"] }
src/main.rs
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Deserialize, Serialize)]
struct Types {
#[serde(rename = "type")]
types: Vec<Type>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
struct Type {
#[serde(rename = "@name")]
name: String,
}
const XML: &str = r#"<types>
<type name="first"/>
<type name="second"/>
</types>"#;
fn main() {
let types: Types = quick_xml::de::from_str(XML).expect("Cannot parse XML.");
dbg!(&types);
match quick_xml::se::to_string(&types) {
Ok(xml) => {
dbg!(xml);
}
Err(error) => {
dbg!(error);
}
};
}
英文:
It's a bug in serde-xml-rs, that cost me half a day.
Using quick-xml
instead of serde-xml-rs
solved the issue:
Cargo.toml
[package]
name = "serde_xml_rs_serialization_error"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = { version = "1.0.164", features = ["derive"] }
quick-xml = { version = "0.28.2", features = ["serialize"] }
src/main.rs
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Deserialize, Serialize)]
struct Types {
#[serde(rename = "type")]
types: Vec<Type>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
struct Type {
#[serde(rename = "@name")]
name: String,
}
const XML: &str = r#"<types>
<type name="first"/>
<type name="second"/>
</types>"#;
fn main() {
let types: Types = quick_xml::de::from_str(XML).expect("Cannot parse XML.");
dbg!(&types);
match quick_xml::se::to_string(&types) {
Ok(xml) => {
dbg!(xml);
}
Err(error) => {
dbg!(error);
}
};
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论