5 Replies - 10744 Views - Last Post: 10 August 2010 - 09:47 AM Rate Topic: -----

#1 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Non white space characters cannot be added to content.

Posted 10 August 2010 - 08:15 AM

I'm trying to grab file paths from a ListBoxItem collection and send them via WebClient. Here is the code I'm using:

private void UploadSelectedImages(ListBox.ObjectCollection objectCollection)
{
    foreach (var photo in objectCollection)
	{
        using (var w = new WebClient())
        {
            var values = new NameValueCollection
            {
                { "key", "<my api here>" },
                { "image", Convert.ToBase64String(File.ReadAllBytes(photo.ToString())) }
            };

            XDocument response = new XDocument(w.UploadValues("http://imgur.com/api/upload.xml", values));
            string name = photo.ToString();
            string smallThumbnail = (string)response.Root.Element("small_thumbnail");
            string actualImage = (string)response.Root.Element("original_image");

            UploadedImage image = new UploadedImage(name, smallThumbnail, actualImage);
            uploadedImages.Add(image);
        }
	}

    foreach (var uploadedImage in uploadedImages)
    {
        MessageBox.Show(uploadedImage.ActualImage);
    }
}


I receive this error when creating the XDocument object. Any ideas on what could be wrong?

I guess my question is [b]How can I convert a byte[] response to an XML document?

This post has been edited by Sergio Tapia: 10 August 2010 - 08:18 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Non white space characters cannot be added to content.

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Non white space characters cannot be added to content.

Posted 10 August 2010 - 08:20 AM

The root entry in your XML document has something in it. IIRC, it can't have attributes or text in it, only elements.
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Non white space characters cannot be added to content.

Posted 10 August 2010 - 08:30 AM

how about this?

http://social.msdn.m...51-32888cbb17d1
Was This Post Helpful? 0
  • +
  • -

#4 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Non white space characters cannot be added to content.

Posted 10 August 2010 - 08:36 AM

That link isn't really useful because it shows how to create a new XDocument from a Dictionary. I need to convert a byte[] 'response' to a string, which will then be loaded to an Xdocument.
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Non white space characters cannot be added to content.

Posted 10 August 2010 - 09:37 AM

how about this...

UTF8Encoding enc = new UTF8Encoding();
string temp = enc.GetString(w.UploadValues("http://imgur.com/api/upload.xml", values));
XDocument response = Xdocument.Parse(tmp);



This will convert the byte array to it's string representation. This is assuming that the data coming back is simply a byte array of XML data.
Was This Post Helpful? 1
  • +
  • -

#6 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Non white space characters cannot be added to content.

Posted 10 August 2010 - 09:47 AM

Thanks, your post gave me a great idea. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1