13

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

5 Answers 5

17
  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!

Sign up to request clarification or add additional context in comments.

1 Comment

omg you amazing! thank you! been trying to do this all night and had it but I was missing the convert.tobase64string() thank you soooooo much!!!!
3

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

Comments

1

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.

Comments

1

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

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.