1

I have been reading this as I would like to do this via LINQ. However, I have been unable to figure out how to read the data from the API.

When I output resource.Data.Body it says, Byte[].

When I output resource.Data.Size it says, 834234822. (or something like that)

I am trying to save the contents into my database like this:

newContent.ATTACHMENT = resource.Data.Body;

However, no data is ever loaded. I assume I have to loop through Body and store the contents in a variable, but I am not sure how.

Can someone help me connect the dots here?

Edit:

This is the source of the binary data I am trying to read http://dev.evernote.com/start/core/resources.php

Edit 2:

I am using the following code which gives me binary data and saves to database, but it must be corrupt, or something because when I go to open the file Windows photo viewer says it's corrupt or too large...

Resource resource = noteStore.getResource(authToken, attachment.Guid, true, false, true, true);

            StringBuilder data = new StringBuilder();

            foreach(byte b in resource.Data.Body)
            {
                data.Append(Convert.ToString(b, 2).PadLeft(8, '0'));  
            }
...
newContent.ATTACHMENT = System.Text.Encoding.ASCII.GetBytes(data.ToString());
Community
  • 1
  • 1
user1477388
  • 20,790
  • 32
  • 144
  • 264

1 Answers1

2

Given that resource.Data.Body is byte[], and newContent.ATTACHMENT is System.Data.Linq.Binary, you should use the constructor on System.Data.Linq.Binary which takes an input parameter of type byte[]. http://msdn.microsoft.com/en-us/library/bb351422.aspx

newContent.ATTACHMENT = new System.Data.Linq.Binary(resource.Data.Body);
Snixtor
  • 4,239
  • 2
  • 31
  • 54
  • I have gotten a little further, please see my update and let me know your opinion. It appears the file binary data is corrupt, so I assume I'm not retrieving it in the proper way. – user1477388 Mar 06 '13 at 21:24
  • Maybe I have the wrong encoding? I am not sure how to check. – user1477388 Mar 06 '13 at 21:29
  • 1
    Why are you now converting `data` to string and then back to ASCII encoded bytes? That operation will *always* corrupt binary data. – Snixtor Mar 06 '13 at 21:30
  • If I just do `newContent.ATTACHMENT = data;` it says, "Cannot implicitly convert type 'System.Text.StringBuilder' to 'System.Data.Linq.Binary'" – user1477388 Mar 06 '13 at 21:32
  • AH HA! I just tried you solution and it worked! Thanks so much. – user1477388 Mar 06 '13 at 21:45