I want to convert a XML file to BSON. then import the BSON to MongoDB. I searched but could not find how to covert this using C#. please provide me a source code to do this using C#
Asked
Active
Viewed 3,442 times
3
-
See the accepted answer [here](http://stackoverflow.com/questions/18611773/dynamic-xml-into-mongodb), also refer [XML to JSON in c#](http://stackoverflow.com/questions/12037085/convert-xml-to-json-using-c-linq) – Nilay Vishwakarma Apr 16 '14 at 10:35
-
Deserialize your XML to a c# object. Then, serialize that object to a collection using the MongoDb drivers. Simplest is to add attributes to the C# class to control the serialization process. After you tried that, if it doesn't work, post more specifics please so we can better help. – WiredPrairie Apr 16 '14 at 11:03
1 Answers
4
Had the same Problem today. It's for sure not the best solution, but i solved it this way in my project and it works for what i need it:
- Deserialize XML to Json
Deserialize Json to Bson
using (var reader = new StreamReader(context.Request.Body)) { var body = reader.ReadToEnd(); // read input string XmlDocument doc = new XmlDocument(); doc.LoadXml(body); // String to XML Document string jsonText = JsonConvert.SerializeXmlNode(doc); //XML to Json var bsdocument = BsonSerializer.Deserialize<BsonDocument>(jsonText); //Deserialize JSON String to BSon Document var mcollection = Program._database.GetCollection<BsonDocument>("test_collection_05"); await mcollection.InsertOneAsync(bsdocument); //Insert into mongoDB }

Tom
- 113
- 8