XML到字典到自定义类实例的字典 [英] XML to Dictionary to Instances of Custom Class

查看:122
本文介绍了XML到字典到自定义类实例的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有比我在下面显示的方法更直接/更优雅的方法来从XML文件中获取数据并从中创建自定义类的实例.下面是我目前的做法(可行,但可能有点笨拙).

I am wondering if there is a more direct/elegant way than what I show below, to take data from an XML file and make instances of a custom class from it. Below is how I current do it (works, but might be a bit clumsy).

我的XML:

<Skills>
  <Fire>
    <Cast>0.00</Cast>
    <ReCast>90.00</ReCast>
    <MPCost>0</MPCost>
    <Button>8</Button>
  </Fire>
  <Ice>
    <Cast>5.98</Cast>
    <ReCast>2.49</ReCast>
    <MPCost>0</MPCost>
    <Button>9</Button>
  </Ice>
</Skills>

1)将XML中的元素加载到字典中:

//Load Skill list
var skillXElement = XDocument.Load(path + @"\Skills.xml").Root;
if (skillXElement != null)
    SkillDictionary =
        skillXElement.Elements()
            .ToDictionary(e => e.Name.LocalName,
                e =>
                    new Skill(e.Name.LocalName, (double) e.Element("Cast"), (double) e.Element("ReCast"),
                        (int) e.Element("MPCost"), e.Element("Button").Value[0]));

2)基于字典创建对技能类的引用:

class SkillInfo
{
    public Skill Fire { get; private set; }
    public Skill Ice { get; private set; }

    public SkillInfo()
    {
        Fire = Globals.Instance.SkillDictionary["Fire"];
        Ice= Globals.Instance.SkillDictionary["Ice"];
    }
}

3)最后,我通过这样的公共场所访问这些技能:

class Player : Character
{
    public Player()
    {
        SkillInfo = new SkillInfo();
    }

    public SkillInfo SkillInfo { get; private set; }

    private ExampleMethod()
    {
        UseSkill(SkillInfo.Fire);
    }
}

您可能想知道为什么我不直接从字典中直接访问每个技能,例如:UseSkill(Globals.Instance.SkillDictionary["Fire"]);.原因是,尽管是O(1),但字典中的每个查找都相当慢(约500毫秒).因此,为避免每次我使用技能时都出现延迟,我创建了SkillInfo类.

You might wonder why I do not just access each skill from the dictionary directly as such: UseSkill(Globals.Instance.SkillDictionary["Fire"]);. The reason is that each lookup in a Dictionary is fairly slow (about 500ms), despite being O(1). So, to avoid that delay each time I use a skill, I created the SkillInfo class.

任何有关如何更优雅地从XML到类实例的技巧或想法都将受到高度赞赏.

Any tips or ideas on how to go from the XML to the class instances more elegantly would be highly appreciated.

提前谢谢!

下面要求的其他信息

  • 我的设置器是私有的,以确保封装.这是我认为可以使用的最严格的访问修饰符.除此之外没有其他要求

推荐答案

只需将MemoryStream替换为StreamReader即可读取文件:

Just replace MemoryStream with a StreamReader to read from a file:

void Main()
{
    var xml = @"<Skills>
                    <Fire>
                        <Cast>0.00</Cast>
                        <ReCast>90.00</ReCast>
                        <MPCost>0</MPCost>
                        <Button>8</Button>
                    </Fire>
                    <Ice>
                        <Cast>5.98</Cast>
                        <ReCast>2.49</ReCast>
                        <MPCost>0</MPCost>
                        <Button>9</Button>
                    </Ice>
                </Skills>";

    using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
    {
        var serializer = new XmlSerializer(typeof(Skills));
        var skills = (Skills)serializer.Deserialize(memoryStream);
        // good to go!
    }
}

public class Skill
{
    public double Cast { get; set; }
    public double ReCast { get; set; }
    public int MPCost { get; set; }
    public int Button { get; set; }
}

public class Skills
{
    public Skill Fire { get; set; }
    public Skill Ice { get; set; }
}

这篇关于XML到字典到自定义类实例的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆