Expecting element 'root' from namespace ''.. Encountered 'None' with name '', namespace ''

12,640

Solution 1

The answer was a very bad mistake on my behalf, but for those who encounter this in future make sure you send your JSON object in Postman through the Raw field. In my case I simply did:

{
  "player_name": "Test",
  "player_location": "EUW",
  "player_wins":10,
  "player_draws":10,
  "player_losses":15,
  "player_points":20
}

This fixed it

Solution 2

I think you marked the wrong variables, this:

[DataMember]
private string _name;
public string player_name 

should be this:

private string _name;
[DataMember]
public string player_name 
Share:
12,640
Halfpint
Author by

Halfpint

#SOreadytohelp

Updated on June 05, 2022

Comments

  • Halfpint
    Halfpint almost 2 years

    I've been searching for the past 4 hours for ways on how to tackle this problem, and I've not yet found a solution.

    I'm building an API with .NET and wish to parse JSON information that is sent back from API calls.

    My current approach does the following:

    private void PostNewPlayer(HttpContext context)
    {
        // Create the serializer
        context.Request.InputStream.Position = 0;
    
        DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(ASPlayer));
        ASPlayer p = (ASPlayer)json.ReadObject(context.Request.InputStream);  <-- Exception here
        Int32 playerId = ASPlayerManager.InsertNewPlayer(p);
    }
    

    But I currently get an Exception at the indicated line. I have made sure my class implements the correct serialization namespaces:

    using System.Runtime.Serialization;
    using System.IO;
    using System.Runtime.Serialization.Json;
    

    The class I am trying to serialize has had its DataContract and Member fields set accordingly:

    [DataContract]
    public class ASPlayer
    {
        [DataMember]
        private string _name;
        public string player_name 
        {
            get { return _name; }
            set { _name = value; }
        }
    
        [DataMember]
        private string _location;
        public string player_location
        {
           get { return _location; }
           set { _location = value; }
        }
    
        // Other vars
        ... 
    
        public ASPlayer(string name, string location)
        {
            _name = name;
            _location = location;
         }
    }
    

    However, when I use a HTTP client such as Postman to make a request I get the error stated in the question title

    enter image description here

  • Halfpint
    Halfpint about 9 years
    Hey. Thanks for the suggestion. I tried marking the variables both ways and still no luck :( any other ideas?
  • thumbmunkeys
    thumbmunkeys about 9 years
    It would help if you post your JSON
  • Halfpint
    Halfpint about 9 years
    I think I've just spotted what I've done wrong. Im sending the parameters in the header, I should be posting a json object. Oops!
  • dbc
    dbc about 9 years
    @thumbmunkeys - ordering is only important for DataContractSerializer. DataContractJsonSerializer ignores property order since JSON object properties are defined to be unordered.