xmlserializer.deserialize()创建新对象而不是读取我想要它读取的对象

时间:2012-11-24 00:44:44

标签: c# wcf wcf-rest

所以当我尝试使用时:

        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;

获得返回剂量的“游戏”不具有与xmlstring“响应”中的游戏相同的属性,它就像:

    game = Serializer.Deserialize(Stream) as Game;

创建游戏对象的新实例,而不是以指定的ID

给我游戏

“字符串响应”锁定如下:

 <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>

但是返回标记处的游戏会像这样锁定

 <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>

游戏课如果有任何帮助:

[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();
    }
}

对不起,如果我给你很多/很少但这是我的第3或第4篇帖子所以我还在学习只是问你是否遗漏了什么

非常感谢您抽出宝贵的时间帮助我!

度过美好的一天!!

2 个答案:

答案 0 :(得分:2)

您可能希望使用DataContractSerializer作为demonstrated here而不是XmlSerializer。

答案 1 :(得分:1)

我尝试使用此扩展方法,它可以正常工作。问题可能是由于XmlSerializer不能与私有字段一起使用。

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);
}