代码之家  ›  专栏  ›  技术社区  ›  Sergio Tapia

准备XML数据进行序列化/反序列化的正确方法是什么

  •  1
  • Sergio Tapia  · 技术社区  · 15 年前
        <?xml version="1.0" encoding="utf-8" ?>
    <Hero>
      <Legion>
        <Andromeda>
          <SpellOne>
            <Name>Comet</Name>
            <Icon>Images/Spell/Andromeda/Spell1.gif</Icon>
            <Action>Target Unit</Action>
            <Description>Andromeda rips a comet from her dimension to hurl at an enemy, damaging and stunning them.</Description>
            <Ranks>
              <First>            
                <ManaCost>95</ManaCost>
                <Cooldown>10 Seconds</Cooldown>
                <Effects>Deals 100 Magic damage and stuns target for 1.75 seconds.</Effects>
                <ExtraEffects></ExtraEffects>
              </First>
              <Second>
                <ManaCost>110</ManaCost>
                <Cooldown>10</Cooldown>
                <Effects>Deals 175 Magic damage and stuns target for 1.75 seconds.</Effects>
                <ExtraEffects></ExtraEffects>
              </Second>
              <Third>
                <ManaCost>125</ManaCost>
                <Cooldown>10</Cooldown>
                <Effects>Deals 250 Magic damage and stuns target for 1.75 seconds.</Effects>
                <ExtraEffects></ExtraEffects>
              </Third>
              <Fourth>
                <ManaCost>140</ManaCost>
                <Cooldown>10</Cooldown>
                <Effects>Deals 325 Magic damage and stuns target for 1.75 seconds.</Effects>
                <ExtraEffects></ExtraEffects>
              </Fourth>
            </Ranks>
          </SpellOne>
    
          <SpellTwo>
            <Name>Aurora</Name>
            <Icon>Images/Spell/Andromeda/Spell2.gif</Icon>
            <Action>Target Position</Action>
            <Description>Andromeda shakes the magnetic field of Newerth, causing an Aurora to erupt, damage, and reduce the armor of all enemies in front of her.</Description>
            <Ranks>
              <First>
                <ManaCost>40</ManaCost>
                <Cooldown>15 Seconds</Cooldown>
                <Effects>Deals 25 damage to targets in a line and applies Aurora for 15 seconds.</Effects>
                <ExtraEffects>-5% Base Damage, -2 Armor</ExtraEffects>
              </First>
              <Second>
                <ManaCost>40</ManaCost>
                <Cooldown>15 Seconds</Cooldown>
                <Effects>Deals 50 damage to targets in a line and applies Aurora for 15 seconds.</Effects>
                <ExtraEffects>-10% Base Damage, -3 Armor</ExtraEffects>
              </Second>
              <Third>
                <ManaCost>40</ManaCost>
                <Cooldown>15 Seconds</Cooldown>
                <Effects>Deals 75 damage to targets in a line and applies Aurora for 15 seconds.</Effects>
                <ExtraEffects>-15% Base Damage, -4 Armor</ExtraEffects>
              </Third>
              <Fourth>
                <ManaCost>40</ManaCost>
                <Cooldown>15 Seconds</Cooldown>
                <Effects>Deals 100 damage to targets in a line and applies Aurora for 15 seconds.</Effects>
                <ExtraEffects>-20% Base Damage, -5 Armor</ExtraEffects>
              </Fourth>
            </Ranks>
          </SpellTwo>
    

    所以我的问题是,我是否正确地格式化了XML?什么是 适当的 这样做的方法。我不介意重新编写1300行XML,只要我学会了为序列化/反序列化准备XML数据的正确方法。

    非常感谢。

    3 回复  |  直到 15 年前
        1
  •  1
  •   Robert Rossney    15 年前
    1. 构建可序列化的对象模型。

    2. 创建并填充它的实例。

    3. 将其序列化为XML。

    现在您知道了输入XML的外观。

    一般来说,只要您不搞砸让您对序列化XML的格式进行控制的各种属性,那么作为值类型的每个属性都将如下所示:

    <PropertyName>ValueSerializedToText</PropertyName>
    

    作为引用类型的每个属性都将如下所示:

    <PropertyName>
        <Property1>Value</Property1>
        <Property2>Value</Property2>
    

    等等…

    因此,在您的示例中,我们希望XML反序列化为类型为的对象 Hero 谁的 Legion 属性包含具有 Andromeda 属性。也就是说,不,这不是XML应该是的样子。

    你真正想要的是 英雄 对象与 Name Faction 属性,可以这样序列化:

    <Hero>
       <Faction>Hellbourne</Faction>
       <Name>Corrupted Disciple</Name>
       ...
    

    但是 所有这些都是在担心错误的事情 . 您不应该将注意力集中在希望XML看起来像什么。您应该关注对象模型的外观。这样做,XML序列化格式将自行处理。

        2
  •  4
  •   taylonr    15 年前

    有两件事可以从XML中跳出来。如果我们讨论的是C序列化,则您的对象必须具有类似于spellOne、spellTwo的属性,以及排名“第一”、“第二”、“第三”和“第四”。

    现在,如果这是在石头上设置的,并且不可能增加/减少等级和或咒语的数量,那可能并不可怕。但是,如果你为某种类型的字符添加了一个拼写3,或者第5等级,你就必须更新你的对象。

    我可能会提出一些建议

    <Spells>
       <Spell id="1" />
       <Spell id="2" /> 
    </Spells>
    

    然后,拼写结构可以是相同的,您可以拥有一个C对象,该对象具有一个列表,然后将被序列化/反序列化。

    除此之外,您的格式看起来还不错。

    您可以尝试创建一个简单的小控制台应用程序,创建您的拼写对象,然后将其转换为XML。看看它输出的结构,这将使您对如何格式化它有一个很好的了解。

        3
  •  2
  •   slugster Joey Cai    15 年前

    我过去做这类事情的一种方法是采用相反的方法——用我满意的结构编写域对象,然后将其序列化为XML。完成之后,我将为它编写一个XSD,以便以后可以方便地手工编辑XML。我采用这种方法是因为拥有域对象的权利更为重要,我并不真正关心XML是什么样子的,尤其是当我有了XSD之后。