0
[DataContract]
public class RootObject
{
    [DataMember]
    public int RecipeID { get; set; }
    [DataMember]
    public string Title { get; set; }
    [DataMember]
    public string Description { get; set; }
    [DataMember]
    public string Cuisine { get; set; }
    [DataMember]
    public string Category { get; set; }
    [DataMember]
}


 public async static Task<RootObject> GetRecipe()
    {
        var http = new HttpClient();
        var url = String.Format("http://api.bigoven.com/recipe/196149?api_key=//apikey");
        var response = await http.GetAsync(url);
        var result = await response.Content.ReadAsStringAsync();
         XmlSerializer serializer = new XmlSerializer(typeof(RootObject));
        var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));
        XmlReader reader = XmlReader.Create(ms);
        RootObject i;
        i = (RootObject)serializer.Deserialize(reader);
        return i;
    }

I use the above method in a bigoven class which gets the data of a recipe as XML data such as:

<Recipe xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<RecipeID>196149</RecipeID>
<Title>Lasagna</Title>
<Cuisine>Italian</Cuisine>

and I would like to put the data for the title and cuisine into textboxes, however when I try the method below I get the error "Recipe xmlns='' was not expected."

        RootObject myRecipe = await bigoven.GetRecipe();
        textBox.Text = myRecipe.Title;
Alexis017
  • 1
  • 1
  • Just curious, you say this data is being returned as XML, then why are you using a JSON serializer? – Ageonix Mar 11 '16 at 20:19
  • You might want to look into the XmlSerializer class. Its Deserialize method takes a Stream and deserializes it into the Object of your choice. – Snake14 Mar 11 '16 at 20:21
  • I've changed it to use an XMLSerialiser but im getting the error: " was not expected." – Alexis017 Mar 11 '16 at 22:19

2 Answers2

0

Answered before. Basically this:

When serialising an XML document to a .NET string, the encoding must be set to UTF-16. Strings are stored as UTF-16 internally, so this is the only encoding that makes sense. If you want to store data in a different encoding, you use a byte array instead.

See the entire thread here:

Using StringWriter for XML Serialization

Community
  • 1
  • 1
Pedro G. Dias
  • 3,162
  • 1
  • 18
  • 30
  • It looks like they are trying to deserialize xml data using a json serializer, hence the invalid "<" character. I'm not sure that's the same thing. They may run into the encoding issue once they use an XmlSerializer, so this may be useful for a different issue. – NickT Mar 11 '16 at 20:13
0

It looks like your Json serializer might be trying to deserialize xml data.

You might try looking at the raw string in result and making sure that it's actually JSON data. If it's not, you need to find the appropriate deserailizer to use.

I'm guessing result contains XML in which case you'd need to use an XML Deserializer to convert it to a RootObject

Here's the link for the .NET one: https://msdn.microsoft.com/en-us/library/tz8csy73(v=vs.110).aspx

NickT
  • 214
  • 1
  • 5
  • I edited my method to try and use it however i get the message, " was not expected." – Alexis017 Mar 11 '16 at 21:34
  • That's definitely XML data though, so I think you're on the right track. – NickT Mar 11 '16 at 22:09
  • result does contain xml as before, but even though I have changed my code to work like the example you provided it is more or less the same error. – Alexis017 Mar 11 '16 at 22:25
  • Well RootObject doesn't contain a property called "Recipe" so the deserializer doesn't know what to do with that node. I don't think you can deserialize the results of that API call to the class you created. You may need to review the API return value and make sure the XML it is returning matches the class you are trying to populate. – NickT Mar 11 '16 at 22:45
  • Additionally, you may want to just call `XDocument.Parse` on the XML string you have and process the XML nodes the way you want. You don't actually have to deserialize it, you can manually process the return value. https://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.parse(v=vs.110).aspx – NickT Mar 11 '16 at 22:48