如果元素名称以句点结尾,则会发生BsonSerializationException [英] BsonSerializationException occurs if element name ends in a period
问题描述
我正在编写一个C#应用程序,该应用程序将从XML文件读取,然后将其转换为JSON,然后上传到MongoDB.我们的某些标签的结构以句号结尾,如下所示:
I am writing a C# application that reads from an XML file, converts that to JSON, and uploads to MongoDB. Some of our tags are structured with a period at the end, like so:
<BatteryTest.>GOOD</BatteryTest.>
使用Newtonsoft库,我可以毫无问题地将XML转换为JSON.当我将其反序列化为BsonDocument时遇到麻烦:
Using the Newtonsoft library I am able to convert the XML to JSON without a problem. It is when I go to deserialize it to a BsonDocument that I have trouble:
var document = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(jsonText);
我收到以下错误消息:
mscorlib.dll中发生类型为'MongoDB.Bson.BsonSerializationException'的异常,但未在用户代码中处理
An exception of type 'MongoDB.Bson.BsonSerializationException' occurred in mscorlib.dll but was not handled in user code
其他信息:元素名称"BatteryTest".无效".
Additional information: Element name 'BatteryTest.' is not valid'.
我查看了文档,但是我还没有发现任何可以解释如何更改解串器格式属性的内容.这是有效的XML,所以我不确定为什么反序列化器也会对此造成阻塞.
I have looked at the documentation but I haven't found anything that would explain how I can change the formatting properties of the deserializer. This is valid XML so I am not sure why the deserializer would choke on it, either.
这是无效的JSON吗?如果是这样,有没有办法在不缩短该期限的情况下将其插入MongoDB?
Is this invalid JSON? If so, is there a way to still insert it into MongoDB without dropping that period?
推荐答案
该点可以在MongoDB中使用查询,不能在字段名称中使用.您必须先对JSON进行预处理,然后再将其转换为BSONDocument
.
As the the dot can be used in MongoDB queries, it can not be used in field names. You will have to preprocess the JSON before converting it into a BSONDocument
.
这篇关于如果元素名称以句点结尾,则会发生BsonSerializationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!