gpt4 book ai didi

c# - 如果元素名称以句点结尾,则发生 BsonSerializationException

转载 作者:可可西里 更新时间:2023-11-01 10:25:34 25 4
gpt4 key购买 nike

我正在编写一个 C# 应用程序,它从 XML 文件中读取数据,将其转换为 JSON,然后上传到 MongoDB。我们的一些标签在末尾带有句点,如下所示:

<BatteryTest.>GOOD</BatteryTest.>

使用 Newtonsoft 库,我可以毫无问题地将 XML 转换为 JSON。当我将它反序列化为 BsonDocument 时,我遇到了麻烦:

var document = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(jsonText);

我收到以下错误消息:

An exception of type 'MongoDB.Bson.BsonSerializationException' occurred in mscorlib.dll but was not handled in user code

Additional information: Element name 'BatteryTest.' is not valid'.

我看过the documentation但我还没有找到任何可以解释如何更改反序列化器的格式属性的内容。这是有效的 XML,所以我也不确定为什么反序列化器会阻塞它。

这是无效的 JSON 吗?如果是这样,有没有办法在不删除那个句点的情况下仍然将它插入到 MongoDB 中?

最佳答案

作为点可以在 MongoDB 中使用 queries , 它不能用于字段名称。在将 JSON 转换为 BSONDocument 之前,您必须对其进行预处理。

关于c# - 如果元素名称以句点结尾,则发生 BsonSerializationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34418691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com