.net - How to store XML in MongoDB? -


context: existing system heavily based on passing xml around in various forms (xmldocument, xdocument/xelement, string encoded). developing new component talk existing system , have it's own data store of kind holding xml later processing. mongodb seems fit data store doesn't have native support xml, i'm wondering options exist handling xml in mongodb.

there 2 options come mind:

1. use xml json converter (for conversion in both directions)

i believe allow querying of data , creation on mongodb indexes. there isn't immediate need lots of querying or lots of different types of querying, @ least have key based retrieval , maybe 1 or 2 queries on values useful (certainly useful keep option open).

is generic xml-2-json converter fit here, or mongodb/bson converter better?

are there specific downsides converting json/bson? ever result in loss of information, perhaps whitespace in blocks of element space mangled?

2. string (or binary) encode xml , store bson byte array.

pros

  • simple.

cons

  • data becomes opaque querying.

are there additional pros/cons above 2 options? there other options available? sane?! (e.g. there better fit problem mongodb?)

=== update ===

a working demo uses newtonsoft.json xml json conversion...

xelement fooelem = xelement.load("foo.xml"); // note. used formatting.indented make json readable debug purposes, otherwise adds unnecessary whitespace characters. string jsonstr = jsonconvert.serializexnode(fooelem, formatting.indented); bsondocument bsondoc = bsondocument.parse(jsonstr); 

from there can call mongodb usual, e.g.:

await collection.insertoneasync(bsondoc); 

this ok/acceptable solution in particular case, more has overhead of converting , parsing json string, unnecessary work. ideally go xelement direct bsondocument.

you make point wrt. avoiding necessity parse json before persisting mongodb, imo. may or may not find commercial .net products (libraries) tackling general problem already.

should go own implementation, fwiw, i've been thinking of document order-friendly general encoding of xml within json, recently, believe round-trippable, or without xml namespaces, , may inspire you.

here's poc, in answer this other question ("xml json mapping challenge") :

https://stackoverflow.com/a/35810403/1409653

'hth,


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -