Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed xml in xml

I need to embed an entire well-formed xml document within another xml document. 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. 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.

The idea that immediately came to mind is to encode the embedded xml in base64, or to zip it. Does this sound ok?

I'm coding in C# by the way.

like image 414
tempy Avatar asked Mar 03 '10 00:03

tempy


4 Answers

You could convert the XML to a byte array, then convert it to binary64 format. That will allow you to nest it in an element, and not have to use CDATA.

like image 191
user284887 Avatar answered Sep 28 '22 01:09

user284887


The W3C-approved way of doing this is XInclude. There is an implementation for .Net at http://mvp-xml.sourceforge.net/xinclude/

like image 25
Dour High Arch Avatar answered Sep 28 '22 01:09

Dour High Arch


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. 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.

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.

Someone, on a similar question recommended ProtoBuf as an option, as well as Fast InfoSet as another option.

like image 23
GrayWizardx Avatar answered Sep 28 '22 01:09

GrayWizardx


Depending on how you construct the XML, one way is to not care about it and let the framework handle it.

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:

<?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>
like image 42
o.k.w Avatar answered Sep 28 '22 01:09

o.k.w