This project has moved. For the latest updates, please go here.

XML -> BSON help

Mar 26, 2010 at 11:43 PM

I am trying to convert XML to BSON.  I am fundamently missing something in the relationship of JSON readers and writters.  Can someone point me to some examples that show deserializing and then serializing between different formats?

//Turn XML to JSON
var json = JsonConvert.SerializeXmlNode(((XDocument)mydoc).GetXmlDocument());

//The WriteRaw() method is does not appear to work yet for the BSONWriter. Does  the JSON reader suck things in to an intermediate JToken format that can be later serialized?
var txtreader = new System.IO.StringReader(json);
var jtxtreader = new JsonTextReader(txtreader);
var ms = new System.IO.MemoryStream(jtxtreader.ReadAsBytes()); //! For some reason the text reader does not like the "{" in line 1 of the JSON document :-(.

//Try and serialize the JToken data?
JsonSerializer seralizer = new JsonSerializer();
var ms2 = new System.IO.MemoryStream();
BsonWriter writer = new BsonWriter(ms2);
seralizer.Serialize(writer, ms);