简体   繁体   English

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

[英]What is the proper way to prepare XML data for serialization/deserialization

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

So my question is, am I formatting my XML correctly? 所以我的问题是,我是否可以正确格式化XML? What is the proper way to do this. 什么是执行此操作的正确方法。 I won't mind rewriting 1300 lines of XML from scratch again as long as I learn the proper way to prepare XML data for serialization/deserialization. 只要我了解准备用于序列化/反序列化的XML数据的正确方法,我都不会介意从头开始重写1300行XML。

Thanks a bunch SO. 非常感谢。

Two things jump out to me at your XML. 您的XML对我来说有两件事。 If we're talking C# serialization, your object would have to have an attribute similar to SpellOne, SpellTwo, as well as Ranks "First", "Second", "Third" and "Fourth". 如果我们在谈论C#序列化,则您的对象必须具有类似于SpellOne,SpellTwo的属性,以及“第一”,“第二”,“第三”和“第四”的等级。

Now, if that is set in stone, and has no possibility of ever increasing/decreasing number of ranks and or spells, that might not be terrible. 现在,如果这是一成不变的,并且不可能增加/减少等级和/或咒语的数量,那可能并不可怕。 But if you Add a SpellThree for some form of character, or a 5th rank, you'd have to update your object. 但是,如果您为某种形式的字符或第5级添加一个SpellThree,则必须更新您的对象。

I might suggest something along the lines of 我可能会提出一些建议

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

Then the spell structure could be the same and you could have a C# object with a List that would then be serialized/deserialized. 然后,拼写结构可能是相同的,并且您可以拥有一个带有List的C#对象,然后将其序列化/反序列化。

Other than that, your format doesn't look too bad. 除此之外,您的格式看起来还不错。

One thing you might try is to create a simple little console app, create your spell Object and then serailze it to XML. 您可以尝试做的一件事是创建一个简单的小型控制台应用程序,创建您的咒语对象,然后将其序列化为XML。 Look at the structure that it outputs, that would give you a good idea of how to format it. 查看它输出的结构,这将使您对如何格式化它有一个很好的了解。

One way i've done this sort of thing in the past is to take the opposite approach - write the domain objects with a structure that i am happy with, then serialize it to XML. 我过去做这种事情的一种方式是采取相反的方法-用我满意的结构编写域对象,然后将其序列化为XML。 Once that is done, i will write an XSD for it so that i can comfortably edit the XML by hand at a later date. 一旦完成,我将为其编写一个XSD,以便以后可以方便地手动编辑XML。 I took this approach because it was more important to have the domain object right, i didn't really care what the XML looked like especially once i had the XSD in place. 我采用这种方法是因为拥有正确的域对象更为重要,我并不真正在乎XML的外观,尤其是当我安装了XSD之后。

  1. Build your serializable object model. 建立您的可序列化对象模型。

  2. Create and populate an instance of it. 创建并填充它的一个实例。

  3. Serialize it to XML. 将其序列化为XML。

Now you know what your input XML will need to look like. 现在您知道了输入XML的外观。

Generally speaking, so long as you don't mess around with the various attributes that let you exert control over the formatting of serialized XML, every property that's a value type will look like this: 一般而言,只要您不弄乱让您对序列化XML的格式施加控制的各种属性,则每个值类型的属性都将如下所示:

<PropertyName>ValueSerializedToText</PropertyName>

and every property that's a reference type will look like this: 每个引用类型的属性都将如下所示:

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

...and so on. ...等等。

So, in your example, one would expect that XML to deserialize into an object of type Hero whose Legion property contained an object that had an Andromeda property. 因此,在您的示例中,人们希望XML反序列化为Hero类型的对象,该对象的Legion属性包含一个具有Andromeda属性的对象。 Which is to say, no, that's not what your XML should look like. 就是说,不,那不是您的XML的样子。

What you're actually going to have is a Hero object with Name and Faction properties, which might be serialized like this: 实际上,您将拥有一个具有NameFaction属性的Hero对象,可以将其序列化为:

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

But all of this is worrying about the wrong thing . 但这一切都在担心错误的事情 You shouldn't be focusing your attention on what you want the XML to look like. 您不应该将注意力集中在想要XML的外观上。 You should focus on what you need your object model to look like. 您应该专注于所需的对象模型外观。 Do this, and the XML serialization format will take care of itself. 这样做,XML序列化格式将自行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM