Embedding Images in XML
One of the really cool features added to XMLSpy a few years ago based on customer requests is the ability to embed external files – such as images – directly in an XML document as encoded text. This gives you the option to package all required data from various external files together in one large XML document. The functionality is also available for embedding images in JSON documents.
Let’s take a look at how easy it is to accomplish this in the XML and JSON editor in just a few steps.

Embed Images in XML or JSON
First, click the Insert / Encoded External File command, which is is available in Text View and Grid View for XML documents, and Text View for JSON files.
Browse to select the file to embed, and then choose either Base 16 or Base 64 encoding. If you wish to enclose the encoded text in an element, check the Create Element check box and specify the name to use.
In this example, we’re creating a new XML element called <photo>. If the Create Element check box is not checked, the encoded text will be inserted directly at the cursor location.
When we click OK, the encoded text of the image file we specified is inserted in the <photo> element and is now a part of our XML document.
Here is a shot of a JSON document in the powerful JSON Grid Editor. You can see an image we embedded in the file.

And that’s all there is to it! You can download a free trial of XMLSpy to try it out.
Как добавить картинку в xml Android Studio?
Добрый день, пытаюсь добавить картинки на стартовый экран игры, но их не видно. так же добавил кнопки(с ними всё в порядке) Что я упускаю?

- Вопрос задан 01 июн. 2022
- 224 просмотра
- Вконтакте

во первых этот xml не соответствует показанному экрану
во вторых тут лучше линер лейаут использовать
в третьих блю принт покажи
скорее всего с alignParent что то напутано, точно не знаю, релатив никто уже не использует
Embedding Images in XML Documents
The technique described in the previous section can be used with any sort of binary data that can be expressed with an array of bytes, including images. Let’s look at how to embed a JPEG image in an XML document.
The structure of the sample XML document is extremely simple. It will consist of a single <jpeg> node holding the BinHex data plus an attribute containing the original name, as shown here:
writer.WriteComment("Contains a BinHex JPEG image"); writer.WriteStartElement("jpeg");
// Get the size of the file
FileInfo fi = new FileInfo(jpegFileName);
FileStream fs = new FileStream(jpegFileName, FileMode.Open); BinaryReader f = new BinaryReader(fs); img = f.ReadBytes(size); f.Close();
// Write the JPEG data writer.WriteBinHex(img, 0, size);
// Close the document writer.WriteEndElement();
This code uses the FileInfo class to determine the size of the JPEG file. FileInfo is a helper class in the System.IO namespace used to retrieve information about individual files. The contents of the JPEG file is extracted using the ReadBytes method of the .NET binary reader. The contents are then encoded as BinHex and written to the XML document. Figure 4-7 shows the source code of the XML just created.
Fie E-ji rinrafc Help
<?30nl version=,Tl. 0 T?>
<!—contains a BinHex jfeg image—> <jpeg arigin4lFileJiamje="univac. jpg"
Or ig i na 1F i 1 e S i ze = ‘T253SZ1>FFDSFFF00Q104A46494S000101C1012C0:i2C 0000FrDB00-430008060607060SOSO7O70709090e0A0Cl40DOC0B0B0Cl9121 30Fl4lDlAlFlElDlAlClC2 02 4 2E272Q222C23lClC28372&2C3C31343434lF 27393D3eS23C^3343ZITOBOO«0iD909O96caiflCl$5DCOD1832211CZ 1323 2323232323232323232323232323232323232323232323232323232323232 323232323232323232323232323232323232FFCCQQllQB0QF5Qlt>A03Q122Q 002lIOi03UOlFFC400iFOOOOOI050lOi0101010100000000000000000I02 ^
Figure 4-7: An XML file containing a BinHex-encoded JPEG file.
The BinHex stream is now part of the XML document and, as such, can be reread using an XML reader and decoded into an array of bytes. The sample application shown in the following code does just that and, in addition, translates the bytes into a Bitmap object to display within a Windows Forms PictureBox control:
XmlTextReader reader = new XmlTextReader(filename);
Filelnfo fi = new Filelnfo(filename); int size = (int) fi.Length; byte[] img = new byte[size]; reader.ReadBinHex(img, 0, size);
// Bytes to Image object MemoryStream ms = new MemoryStream(); ms.Write(img, 0, img.Length); Bitmap bmp = new Bitmap(ms); ms.Close();
// Fill the PictureBox control Jpeglmage.Image = bmp;
The reader opens the XML file and jumps to the root node using MoveToContent. Next it gets the size of the XML file to oversize the buffer destined to contain the decoded JPEG file. Bear in mind that a BinHex stream is always significantly larger then a binary JPEG file, but this is the price you must pay to string encoding algorithms. The ReadBinHex method decodes the JPEG stream and stores it in a MemoryStream object. This step is necessary if you want to transform the array of bytes into a .NET Framework graphics object—say, the Bitmap object—that can be then bound to a PictureBox control, as shown in Figure 4-8.
Figure 4-8: A PictureBox control displays a JPEG file just extracted from an XML file and properly decoded.
If you want to extract the image bits and create a brand-new JPEG file, use the following code. The name of the JPEG file is read out of the OriginalFileName attribute in the XML encoded document.
string originalFileName = reader["OriginalFileName"]; FileStream fs = new FileStream(originalFileName,
How do you add an image?
I have a simple XML document that contains image information. I need to transform it into HTML. However, I can’t see where the open tag is and when I use the XSL code below, it shows the following error message:
«Cannot write an attribute node when no element start tag is open.»
XML content:
XSL code:
5 Answers 5
Just to clarify the problem here — the error is in the following bit of code:
The instruction xsl:copy-of takes a node or node-set and makes a copy of it — outputting a node or node-set. However an attribute cannot contain a node, only a textual value, so xsl:value-of would be a possible solution (as this returns the textual value of a node or nodeset).
A MUCH shorter solution (and perhaps more elegant) would be the following:
The use of the <> in the attribute is called an Attribute Value Template, and can contain any XPATH expression.
Note, the same XPath can be used here as you have used in the xsl_copy-of as it knows to take the textual value when used in a Attribute Value Template.