如何调试XML反序列化?

时间:2014-09-21 16:12:54

标签: c# xml serialization xml-deserialization

我想知道您是否有关于如何调试以下XML反序列化的任何提示?我无法让它发挥作用。反序列化器基本上创建了summon和slash实例,但它们的所有属性都是空的。相关课程如下所示。


具有反序列化程序的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)实例,并能够通过它们的属性分别访问它们。

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

1 个答案:

答案 0 :(得分:1)

的Anders,

我最近做了类似的任务。看看你的代码,你似乎设置了DataContract Name和Namespace错误。要在Skill类和SkillCollection类之上指定:[DataContract(Name = "Skills", Namespace = "")]。这对于“允许”进入属性的序列化程序至关重要。请记住,仅将其设置在SkillCollection类之上是不够的。

编辑:我最初认为你可以省略Namespace,但你不能。如果您没有在XML中指定命名空间,则必须将其设置为Namespace="",或者在指定命名空间的情况下,必须设置为 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; } }

另外,调试它的方法是将Deser方法更改为:

{{1}}