How to debug XML deserialization?

I was wondering if you have any tips on how I can debug deserialization below XML? I can not make it work. Deserializer basically creates instances of summon and slash, but all of their properties are empty. Below are the relevant classes.


SkillCollection class with Deserializer:

[DataContract(Name = "Skills", Namespace = "")]
public class SkillCollection
{

[DataMember(Name = "Slash")]
public Skill Slash { get; set; }

[DataMember(Name = "Summon")]
public Skill Summon { get; set; }

public static object Deser(string path, Type toType)
{
    var s = new DataContractSerializer(toType);
    using (FileStream fs = File.Open(path, FileMode.Open))
    {
        object s2 = s.ReadObject(fs);
        if (s2 == null)
            Console.WriteLine(@"  Deserialized object is null");
        else
            Console.WriteLine(@"  Deserialized type: {0}", s2.GetType());
        return s2;
    }
}

It is called from another class through the Skills property:

Skills = (SkillCollection)SkillCollection.Deser(
            Path.Combine(path, "Skills.xml"), 
            typeof(SkillCollection));

Skill Class:

public class Skill
{
    //Cast: time it takes to cast it
    [DataMember(Name = "Cast")]
    public float Cast { get; set; }

    //ReCast: cooldown period before player can cast it again
    [DataMember(Name = "ReCast")]
    public float ReCast { get; set; }

    [DataMember(Name = "MPCost")]
    public int MpCost { get; set; }

    public Timer Timer { get; private set; }
    public bool Ready { get; set; }

    public Skill()
    {
        Ready = true;

        Timer = new Timer { Interval = ReCast + 500, AutoReset = false };
        Timer.Elapsed += OnTimedEvent;
    }

    //Runs when recast is up
    private void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        Ready = true;
    }
}

XML file:

<Skills>
  <Slash>
    <Cast>0.00</Cast>
    <ReCast>60.00</ReCast>
    <MPCost>0</MPCost>
  </Slash>
  <Summon>
    <Cast>5.98</Cast>
    <ReCast>2.49</ReCast>
    <MPCost>0</MPCost>
  </Summon>
</Skills>

Just because there is no confusion, my goal is to start the deserializer, and then the SkillCollection class contains two Skill instances (Slash and Summon) and have access to them separately through their properties.

Thanks for any help / debugging tips.

+4
source share
1

,

. , , DataContract Name Namespace. Skill, SkillCollection : [DataContract(Name = "Skills", Namespace = "")]. , "" . , SkillCollection .

EDIT: , Namespace, . Namespace="", XML , , , .

, , Deser :

    public static object Deserialize(string path, Type toType)
    {
        using (var sr = new FileStream(path, FileMode.Open))
        {
            SkillCollection p = null;
            XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(sr, new XmlDictionaryReaderQuotas());
            var serializer = new DataContractSerializer(toType);

            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        if (serializer.IsStartObject(reader))
                        {
                            Console.WriteLine(@"Found the element");
                            p = (SkillCollection)serializer.ReadObject(reader);
                            Console.WriteLine("{0} {1}    id:{2}",
                                p.Slash.ToString(), p.Summon.ToString());
                        }
                        Console.WriteLine(reader.Name);
                        break;
                }
            }

            return p;
        }
    }
0

All Articles