0

So lately I have been using this method to return a string:

string uri = "http://localhost:8000/Service/Student";
XDocument xDoc = XDocument.Load(uri);
var studentcollection = xDoc.Descendants("Student")
    .Select(n => new
    {
      FirstName = n.Element("FirstName").Value,
    })
    .ToList();

And it works fine, but if one of the values from the webservice is a byte[] will this method return that value? If not, how would you combine that method (because im use to it) and return a byte[]?

3
  • Im tempted to say .Value.ToArray? would that be wrong Commented Apr 24, 2012 at 7:00
  • 2
    possible duplicate of Extract a Byte[] from an XElement with Linq to Xml Commented Apr 24, 2012 at 7:03
  • It "could" possibly be a duplicate im not entirely sure, from my webservice I already convert to base64 but I really dont want to convert the byte[], I would like to get it back unconverted. Commented Apr 24, 2012 at 7:52

1 Answer 1

1

Are you saying that one of the elements could return a byte[] or is it really a base 64 encoded string representing a byte[]? If the latter, you can extract the bytes from the value:

byte[] decoded = Convert.FromBase64String(value);

Full code:

string uri = "http://localhost:8000/Service/Student";
XDocument xDoc = XDocument.Load(uri);
var studentcollection = xDoc.Descendants("Student")
    .Select(Convert.FromBase64String(n.Element("Picture").Value))
    .ToList();

Which will give you a list of byte[] extracted out of the 'Picture' element for each student (change code accordingly to create Picture as part of your student instance).

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

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.