Insert image into xml file using c#

回眸只為那壹抹淺笑 提交于 2020-01-21 01:34:30

问题


I've looked everywhere for the answer to this question but cant find anything so hoping you guys can help me on here.

Basically I want to insert an image into an element in xml document that i have using c#

I understand i have to turn it into bytes but im unsure of how to do this and then insert it into the correct element...

please help as i am a newbie


回答1:


  1. Read all the bytes into memory using File.ReadAllBytes().
  2. Convert the bytes to a Base64 string using Convert.ToBase64String().
  3. Write the Base64 Encoded string to your element content.

Doneski!




回答2:


Here's an example in C# for writing and reading images to/from XML.




回答3:


You can use a CDATA part or simply put all the bytes in their hexadecimal form as a string. Another option is to use a base64 encoding

The element you use is up to you.




回答4:


http://www.dreamincode.net/code/snippet1335.htm seems to do exactly what you want to do. It might be something you might want to try out. Note that it is in VB.NET which you can easily convert to C#.




回答5:


XML can only contain characters, it can't contain an image. There are various ways you can represent an image using characters, for example by encoding the image in PNG and then encoding the PNG in base64; or you could generate an element that contains a link to a URI from where the image can be retrieved. All such conventions have to be agreed between sender and recipient. So before you rush into base64 encoding, check that this is what the recipient expects.



来源:https://stackoverflow.com/questions/4776645/insert-image-into-xml-file-using-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!