As a continuation of this question, I cannot figure out how to remove the period from all my field names in the JSON input.
I convert the XML to JSON and create a BsonDocument to insert into the MongoDB database using the Newtonsoft library as follows:
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
String jsonText = JsonConvert.SerializeXmlNode(doc);
BsonDocument = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(jsonText);
I cannot insert this because I will get an exception for serialization, because the element name contains a period. How can I process either a JSON string or a BsonDocument to modify them?
I successfully repeated 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 that I can set, only get. How can I update a BsonDocument or JSON string to remove invalid field names?