Search code examples
c#wcfwcf-rest

xmlserializer.deserialize() Creates new object instead of reading the one i want it to read


So when i try to use:

        WebRequest request = WebRequest.Create("http://localhost:9998/API/GetGameById/" + ID.ToString());
        WebResponse ws = request.GetResponse();
        StreamReader responseStream = new StreamReader(ws.GetResponseStream());
        string response = responseStream.ReadToEnd();

        Game game;

        using (MemoryStream Stream = new MemoryStream(UTF8Encoding.Default.GetBytes(response)))
        {
            XmlSerializer Serializer = new XmlSerializer(typeof(Game));
            game = Serializer.Deserialize(Stream) as Game;
        }
        responseStream.Close();
        return game;

the "game" that get's returned dose not have the same properties as the game in the xmlstring "response", it's like the :

    game = Serializer.Deserialize(Stream) as Game;

creates a new instance of a game object instead of giving me the game at the specified ID

The "string response" locks like this:

 <Game xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Gameboard i:nil="true" xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
    <Id>1</Id>
    <Players xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
       <a:int>1</a:int>
    </Players>
    <hsize>0</hsize>
    <vsize>0</vsize>
    <winner>0</winner>
 </Game>

but the game at the return marker locks like this

 <Game xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Gameboard i:nil="true" xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
    <Id>0</Id>
    <Players i:nil="true" xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
    <hsize>0</hsize>
    <vsize>0</vsize>
    <winner>0</winner>
 </Game>

Game class if it's of any help:

[DataContract(Namespace = "")]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class Game
{
    [DataMember]
    int Id;
    [DataMember]
    int[] Players;
    [DataMember]
    int[] Gameboard;
    [DataMember]
    int hsize;
    [DataMember]
    int vsize;
    [DataMember]
    int winner = 0;

    public Game()
    { }

    public Game(int newgameID)
    {
        Id = ++newgameID;
    }

    public int GetSetId
    {
        get { return Id; }
        set { Id = value; }
    }
    public int[] GetSetGameboard
    {
        get { return Gameboard; }
        set { Gameboard = value; }
    }
    public int GetSetwinner
    {
        get { return winner; }
        set { winner = value; }
    }
    public int[] GetPlayerList
    {
        get { return Players; }
    }

    public void AddPlayer()
    {
        int NewPlayer;
        if (Players == null)
            Players = new int[0];
        List<int> temp = Players.ToList();
        if (temp.Count == 0)
            NewPlayer = 1;
        else
        {
            NewPlayer = temp.Last();
            NewPlayer++;
        }
        temp.Add(NewPlayer);
        Players = temp.ToArray();
    }
}

sorry if i gave you to much/little but this is my 3 or 4th post so i'm still learning just ask if your missing something

Thank you so much for taking your time and helping me out!

Have a great day!!


Solution

  • I tried with this extension method and it works a expected. The problem might be due to that the XmlSerializer does not work with private fields.

    public static T DeserializeWithDataContractSerializer<T>(this string xml)
    {
        var dataContractSerializer = new DataContractSerializer(typeof(T));
        using (var reader = new XmlTextReader( new StringReader(xml)))
        {
            return (T)dataContractSerializer.ReadObject(reader);
        }
    }
    
    [Test]
    public void GameTest()
    {
        string xml =
            @" <Game xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"">
                    <Gameboard i:nil=""true"" xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""/>
                    <Id>1</Id>
                    <Players xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"">
                       <a:int>1</a:int>
                    </Players>
                    <hsize>0</hsize>
                    <vsize>0</vsize>
                    <winner>0</winner>
                 </Game>";
        var game = xml.DeserializeWithDataContractSerializer<Game>();
        Assert.AreEqual(1,game.GetSetId);
    }