views:

96

answers:

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

Thanks a bunch SO.

+3  A: 

Two things jump out to me at your 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".

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.

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.

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. Look at the structure that it outputs, that would give you a good idea of how to format it.

taylonr
+2  A: 

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

slugster
+1  A: 
  1. Build your serializable object model.

  2. Create and populate an instance of it.

  3. Serialize it to XML.

Now you know what your input XML will need to look like.

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:

<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. Which is to say, no, that's not what your XML should look like.

What you're actually going to have is a Hero object with Name and Faction properties, which might be serialized like this:

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

Robert Rossney
Your answer offered great input and exactly the type ear pulling I needed. I have one doubt though. All of the information I'm saving to the XML is hand transcribed from a wiki website. I don't really understand what you mean by "focus on what you need your object model to look like". Are the two mutually exclusive?
Sergio Tapia
The point of your program is to do something with this data, right? Design the program to do whatever it's going to do without worrying about what the XML it's going to read is going to look like. That's an implementation detail that is very low on the list of things you should be caring about.What are you writing the program for? I mean, I know it's for HoN, as you can probably tell from my examples, but what's its purpose?
Robert Rossney