As a follow up to this question I cannot figure out how to remove a period from all of my field names in JSON input.
I am converting XML to JSON and creating a BsonDocument to be inserted into a MongoDB database using the Newtonsoft library like this:
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
String jsonText = JsonConvert.SerializeXmlNode(doc);
BsonDocument = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(jsonText);
I can't insert this because I'll get a serialization exception since the element name contains a period. How can I process through either the JSON string or the BsonDocument to change them?
I have successfully iterated through my document recursively:
private void Print(BsonDocument document)
{
foreach (BsonElement element in document)
{
Console.WriteLine(element.Name);
if (element.Value.IsBsonDocument)
{
Print(element.Value.AsBsonDocument);
}
else if (element.Value.IsBsonArray)
{
var array = element.Value.AsBsonArray;
foreach (BsonDocument doc in array)
{
Print(doc);
}
}
}
}
However, BsonDocument.Name is not a field I can set, only get. How can I update the BsonDocument or the JSON string to remove the invalid field names?