简体   繁体   English

如何在xml中嵌入xml

[英]How to embed xml in xml

I need to embed an entire well-formed xml document within another xml document. 我需要在另一个xml文档中嵌入一个格式良好的xml文档。 However, I would rather avoid CDATA (personal distaste) and also I would like to avoid the parser that will receive the whole document from wasting time parsing the embedded xml. 但是,我宁愿避免使用CDATA(个人厌恶),也希望避免因为浪费时间解析嵌入式xml而收到整个文档的解析器。 The embedded xml could be quite significant, and I would like the code that will receive the whole file to treat the embedded xml as arbitrary data. 嵌入式xml可能非常重要,我希望接收整个文件的代码将嵌入式xml视为任意数据。

The idea that immediately came to mind is to encode the embedded xml in base64, or to zip it. 立即想到的想法是在base64中对嵌入式xml进行编码,或者对其进行压缩。 Does this sound ok? 听起来不错吗?

I'm coding in C# by the way. 我顺便用C#编写代码。

You could convert the XML to a byte array, then convert it to binary64 format. 您可以将XML转换为字节数组,然后将其转换为binary64格式。 That will allow you to nest it in an element, and not have to use CDATA. 这将允许您将其嵌套在一个元素中,而不必使用CDATA。

The W3C-approved way of doing this is XInclude. W3C批准的方法是XInclude。 There is an implementation for .Net at http://mvp-xml.sourceforge.net/xinclude/ .Net的实现在http://mvp-xml.sourceforge.net/xinclude/

Just a quick note, I have gone the base64 route and it works just fine but it does come with a stiff performance penalty, especially under heavy usage. 只是一个简单的说明,我已经走了base64路线并且它工作得很好但它确实带来了严重的性能损失,特别是在大量使用的情况下。 We do this with document fragments upto 20MB and after base64 encoding they can take upwards of 65MB (with tags and data), even with zipping. 我们这样做的文档片段高达20MB,在base64编码后,它们可以占用65MB(带标签和数据),即使是压缩。

However, the bigger issue is that .NET base64 encoding can consume up-to 10x the memory when performing the encoding/decoding and can frequently cause OOM exceptions if done repeatedly and/or done on multiple threads. 但是,更大的问题是.NET base64编码在执行编码/解码时可能会消耗高达10倍的内存,并且如果在多个线程上重复执行和/或完成,则可能经常导致OOM异常。

Someone, on a similar question recommended ProtoBuf as an option, as well as Fast InfoSet as another option. 有人在类似问题上推荐ProtoBuf作为选项,以及Fast InfoSet作为另一种选择。

Depending on how you construct the XML, one way is to not care about it and let the framework handle it. 根据您构建XML的方式,一种方法是不关心它并让框架处理它。

XmlDocument doc = new XmlDocument(); 
doc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\" ?><helloworld></helloworld>");
string xml = "<how><are><you reply=\"i am fine\">really</you></are></how>";
doc.GetElementsByTagName("helloworld")[0].InnerText = xml;

The output will be something like a HTMLEncoded string: 输出将类似于HTMLEncoded字符串:

<?xml version="1.0" encoding="utf-8"?>
<helloworld>&lt;how&gt;&lt;are&gt;&lt;you
  reply="i am fine"&gt;really&lt;/you&gt;&lt;/are&gt;&lt;/how&gt;
</helloworld>

我会用你喜欢的方式编码它(例如base64或HttpServerUtility :: UrlEncode,...),然后嵌入它。

If you don't need the xml declaration (first line of the document), just insert the root element (with all childs) into the tree of the other xml document as a child of an existing element. 如果您不需要xml声明(文档的第一行),只需将根元素(包含所有子元素)作为现有元素的子元素插入到另一个xml文档的树中。 Use a different namespace to seperate the inserted elements. 使用不同的命名空间分隔插入的元素。

似乎序列化是推荐的方法。

Can't you use XSLT for this? 你不能用XSLT吗? Perhaps using xsl:copy or xsl:copy-of? 也许使用xsl:copy或xsl:copy-of? This is what XSLT is for. 这就是XSLT的用途。

I use Comments for this : 我为此使用评论:

<!-- your xml text --> <! - 你的xml文本 - >

[EDITED] [EDITED]
If the embedded xml with comments, replace it with a different syntax. 如果嵌入了带注释的xml,请用不同的语法替换它。

<?xml version="1.0" encoding="iso-8859-1" ?>
<xml>
    <status code="0" msg="" cause="" />
    <data>
        <order type="07" user="none" attrib="..." >
        <xmlembeded >
            <!--
                <?xml version="1.0" encoding="iso-8859-1" ?>
                <xml>
                <status ret="000 "/>
                <data>
                <allxml_here />
                <!** embedeb comments **>
                </data>
                <xml>
            -->
        </xmlembeded >
        </order>
        <context sessionid="12345678" scriptname="/from/..."  attrib="..." />
    </data>
</xml>

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

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