简体   繁体   English

如何在MongoDB中存储XML?

[英]How to store XML in MongoDB?

Context: An existing system that is heavily based on passing XML around in various forms (XmlDocument, XDocument/XElement, string encoded). 上下文:一种现有系统,它基于以各种形式传递XML(XmlDocument,XDocument / XElement,字符串编码)。 We are developing a new component that will talk to the existing system and will have it's own data store of some kind for holding XML for later processing. 我们正在开发一个新组件,它将与现有系统进行通信,并拥有自己的某种数据存储,用于保存XML以供以后处理。 MongoDB seems like a good fit for the data store but it doesn't have native support for XML, so I'm wondering what good options exist for handling XML in MongoDB. MongoDB似乎非常适合数据存储,但它没有对XML的原生支持,所以我想知道在MongoDB中处理XML有什么好的选择。

There are two options that come to mind: 有两种选择可以想到:

1. Use an XML to JSON converter (for conversion in both directions) 1.使用XML到JSON转换器(双向转换)

I believe this will allow querying of the data and the creation on MongoDB indexes. 我相信这将允许查询MongoDB索引上的数据和创建。 There isn't an immediate need to do lots of querying or lots of different types of querying, but we would at the very least have to do some key based retrieval and maybe one or two queries on the values would be useful (certainly useful to keep that option open). 没有立即需要进行大量的查询或许多不同类型的查询,但我们至少必须做一些基于密钥的检索,并且可能对值有一两个查询是有用的(当然对于保持该选项开放)。

Is a generic XML-2-JSON converter a good fit here, or would a MongoDB/BSON converter be better? 通用的XML-2-JSON转换器是否适合这里,或者MongoDB / BSON转换器会更好?

Are there any specific downsides to converting to JSON/BSON? 转换为JSON / BSON有什么特别的缺点吗? Could it ever result in loss of information, perhaps whitespace in blocks of element space could get mangled? 它是否会导致信息丢失,或许元素空间块中的空白可能会被破坏?

2. String (or binary) encode the XML and store it as a BSON byte array. 2.字符串(或二进制)对XML进行编码并将其存储为BSON字节数组。

Pros 优点

  • Simple. 简单。

Cons 缺点

  • Data becomes opaque to querying. 数据对查询变得不透明。

Are there additional pros/cons to the above two options? 上述两种选择还有其他优缺点吗? Are there other options available? 还有其他选择吗? Is this sane?! 这是理智的吗?! (eg is there a better fit for this problem than MongoDB?) (例如,有没有比MongoDB更适合这个问题?)

=== UPDATE === ===更新===

A working demo that uses Newtonsoft.Json for the XML to JSON conversion... 一个使用Newtonsoft.Json进行XML到JSON转换的工作演示......

XElement fooElem = XElement.Load("foo.xml");
// Note. I used Formatting.Indented to make the JSON easily readable for debug purposes, otherwise it just adds unnecessary whitespace characters.
string jsonStr = JsonConvert.SerializeXNode(fooElem, Formatting.Indented);
BsonDocument bsonDoc = BsonDocument.Parse(jsonStr);

From there you can just call MongoDB as usual, eg: 从那里你可以照常调用MongoDB,例如:

await collection.InsertOneAsync(bsonDoc);

This is probably an OK/acceptable solution in my particular case, but more generally it has the overhead of converting to and then parsing a JSON string, which is unnecessary work. 在我的特定情况下,这可能是一个好的/可接受的解决方案,但更一般地说它有转换然后解析JSON字符串的开销,这是不必要的工作。 Ideally we would go from XElement direct to a BsonDocument. 理想情况下,我们会从XElement直接转到BsonDocument。

You make a good point wrt. 你提出了一个很好的观点。 avoiding the necessity to parse JSON before persisting it to MongoDB, imo. 避免在将JSON持久化到MongoDB之前解析JSON的必要性。 You may or may not find commercial .NET products (libraries) tackling this general problem already. 您可能已经或可能没有找到解决此一般问题的商业.NET产品(库)。

Should you go for your own implementation, FWIW, I've been thinking of a document order-friendly general encoding of XML within JSON, recently, which I believe is round-trippable, with or without XML namespaces, and may inspire you. 你是否应该自己实现FWIW,我最近一直在考虑在JSON中使用文档顺序友好的XML通用编码,我认为这种编码是圆形的,有或没有XML命名空间,可能会激发你的灵感。

Here's the PoC, in my answer to this other question ("xml to json mapping challenge") : 这是PoC,在我对这个问题的回答中(“xml to json mapping challenge”)

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

'HTH, “HTH,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM