如何调试XML反序列化

本文关键字:XML 反序列化 调试 何调试 | 更新日期: 2023-09-27 18:23:59

我想知道您是否有关于如何调试以下XML反序列化的提示?我不能让它工作。反序列化程序基本上创建了调用和斜杠实例,但它们的所有属性都是空的。到的相关类别如下所示。


带有反序列化程序的SkillCollection类:

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

它是通过属性Skills:从另一个类调用的

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

技能等级:

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文件:

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

为了避免混淆,我的目标是运行反序列化程序,然后让SkillCollection类包含Skill的两个实例(Slash和Summon),并能够通过它们的属性分别访问它们。

感谢您提供的任何调试帮助/提示。

如何调试XML反序列化

安德斯,

我最近做了一项类似的任务。查看您的代码,您似乎将DataContract名称和命名空间设置错了。在Skill类和SkillCollection类之上都需要指定:[DataContract(Name = "Skills", Namespace = "")]。这对于"允许"序列化程序输入Properties至关重要。请记住,仅仅将其设置在SkillCollection类之上是不够的。

编辑:我最初以为你可以省略Namespace,但你不能。如果您没有在XML中指定名称空间,则必须将其设置为Namespace="",如果您指定了名称空间,那么必须将其设为指定的名称空间。

此外,调试它的一种方法是将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;
        }
    }