将图像插入 XML 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/746736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert images to XML file
提问by Nguyen Quoc Hung
I would like to insert an images within an element the XML file, what is the best way to do this? Would you please suggest some good way to include an images to xml file?
我想在 XML 文件的元素中插入图像,最好的方法是什么?您能否建议一些将图像包含到 xml 文件的好方法?
回答by Marc Gravell
The most common way of doing this is to include the binary as base-64 in an element. However, this is a workaround, and adds a bit of volume to the file.
执行此操作的最常见方法是将二进制文件作为 base-64 包含在元素中。但是,这是一种解决方法,并且会为文件增加一些音量。
For example, this is the bytes 00 to 09 (note we needed 16 bytes to encode 10 bytes worth of data):
例如,这是字节 00 到 09(注意我们需要 16 个字节来编码 10 个字节的数据):
<xml><image>AAECAwQFBgcICQ==</image></xml>
howyou do this encoding varies per architecture. For example, with .NET you might use Convert.ToBase64String, or XmlWriter.WriteBase64.
您如何进行此编码因架构而异。例如,对于 .NET,您可以使用Convert.ToBase64String, 或XmlWriter.WriteBase64.
回答by Wim Haanstra
I always convert the byte data to a Base64 encoding and then insert the image.
我总是将字节数据转换为 Base64 编码,然后插入图像。
This is also the way that Word does it, for it's XML files (not that Word is a good example on how to work with XML :P).
这也是 Word 的处理方式,因为它是 XML 文件(不是 Word 是如何使用 XML 的一个很好的例子:P)。
回答by Joey
Since XML is a text format and images are usually not (except some ancient and archaic formats) there is no really sensible way to do it. Looking at things like ODT or OOXML also shows you that they don't embed images directly into XML.
由于 XML 是一种文本格式,而图像通常不是(除了一些古老和过时的格式),因此没有真正明智的方法来做到这一点。查看诸如 ODT 或 OOXML 之类的内容还表明它们不会将图像直接嵌入到 XML 中。
What you can do, however, is convert it to Base64 or similar and embed it into the XML.
但是,您可以做的是将其转换为 Base64 或类似格式并将其嵌入到 XML 中。
XML's whitespace handling may further complicate things in such cases, though.
不过,在这种情况下,XML 的空白处理可能会使事情进一步复杂化。
回答by Slavo
XML is not a format for storing images, neither binary data. I think it all depends on how you want to use those images. If you are in a web application and would want to read them from there and display them, I would store the URLs. If you need to send them to another web endpoint, I would serialize them, rather than persisting manually in XML. Please explain what is the scenario.
XML 不是用于存储图像的格式,也不是二进制数据。我认为这完全取决于您想如何使用这些图像。如果您在 Web 应用程序中并希望从那里读取它们并显示它们,我会存储这些 URL。如果您需要将它们发送到另一个 Web 端点,我会将它们序列化,而不是手动保存在 XML 中。请解释一下是什么场景。
回答by Rad
Here's some code taken from Kirk Evans Blogthat demonstrates how to encode an image in C#;
下面是一些取自Kirk Evans 博客的代码,它演示了如何在 C# 中对图像进行编码;
//Load the picture from a file
Image picture = Image.FromFile(@"c:\temp\test.gif");
//Create an in-memory stream to hold the picture's bytes
System.IO.MemoryStream pictureAsStream = new System.IO.MemoryStream();
picture.Save(pictureAsStream, System.Drawing.Imaging.ImageFormat.Gif);
//Rewind the stream back to the beginning
pictureAsStream.Position = 0;
//Get the stream as an array of bytes
byte[] pictureAsBytes = pictureAsStream.ToArray();
//Create an XmlTextWriter to write the XML somewhere... here, I just chose
//to stream out to the Console output stream
System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(Console.Out);
//Write the root element of the XML document and the base64 encoded data
writer.WriteStartElement("w", "binData",
"http://schemas.microsoft.com/office/word/2003/wordml");
writer.WriteBase64(pictureAsBytes, 0, pictureAsBytes.Length);
writer.WriteEndElement();
writer.Flush();

