BsonSerializationException occurs if element name ends in a period

那年仲夏 提交于 2019-12-12 19:22:59

问题


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.>

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);

I get the following error message:

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'.

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.

Is this invalid JSON? If so, is there a way to still insert it into MongoDB without dropping that period?


回答1:


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.



来源:https://stackoverflow.com/questions/34418691/bsonserializationexception-occurs-if-element-name-ends-in-a-period

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!