0

I'am extremely new when it comes to creating and using XML files.

Say for example I have this code for storing highscores

    [Serializable]
    public class HighScoreData
    {
        public string[] PlayerName;
        public int[] Score;
        public int[] Level;
    }

and I want to create xml code to that when it deserializes it'll look something like this

playername= {"Rocco","Shawn","Derrick"}

score= {100,200,300}

level = {1,2,3}

What would that xml code look like?

4
  • 1
    That output isn't in XML format yet the question title has XML in the name. Did you want this to be the content of an XML tag? Or something else? Commented Aug 8, 2013 at 20:42
  • 2
    What you want looks more like JSON than XML.. Anyway, you can use methods of XmlSerializer class for this. Commented Aug 8, 2013 at 20:42
  • I already had the code to deserialize it. I just want to know what the xml file should look like. For example I have an xml file called HighScores.xml how should that file be formatted? @ago yeah something like that. basically I'm just want to manual write the XML code so that i can create something like this HighScoreData scores= new HighScoreData(); and when I look at scores.playername[0] i get "Rocco" Commented Aug 8, 2013 at 20:45
  • If you're looking for a custom format, you can implmenet ISerializable. Commented Aug 8, 2013 at 20:46

3 Answers 3

4

Why did you model it like that? In my opinion the following example is more compelling.

[Serializable]
public class HighScoreData
{
    public string PlayerName;
    public int Score;
    public int Level;
}

[Serializable]
public class HighScoresCollection
{
    List<HighScoreData> HighScores; 
}

Then, when you serialize the highScore, you get something like this:

<HighScoresCollection>
    <HighScoreData>
        <PlayerName>Rocco</PlayerName>
        <Score>100</Score>
        <Level>1</Level>
    </HighScoreData>
    <HighScoreData>
        <PlayerName>Shawn</PlayerName>
        <Score>200</Score>
        <Level>2</Level>
    </HighScoreData>
    <HighScoreData>
        <PlayerName>Derrick</PlayerName>
        <Score>300</Score>
        <Level>3</Level>
    </HighScoreData>
</HighScoresCollection>

Well, the need for the HighScoresCollection class in my example is arguable, and you can just have a list of scores, IMO.

Sign up to request clarification or add additional context in comments.

1 Comment

The HighScoresCollection or any other single root element allows for dumping the xml into a standalone file and may also serve as a hook for namespace declarations.
0

I doubt you want to have single object with multiple members (Arrays). I think you need an Array of objects. Anyway for your sample it is:

<HighScoreData>
    <ArrayOfPlayerName>
        <PlayerName>Rocco</PlayerName>
        <PlayerName>Shawn</PlayerName>
        <PlayerName>Derrick</PlayerName>
    </ArrayOfPlayerName>
    <ArrayOfScore>
        <Score>100</Score>
        <Score>200</Score>
        <Score>300</Score>
    </ArrayOfScore>
    <ArrayOfLevel>
        <Level>1</Level>
        <Level>2</Level>
        <Level>3</Level>
    </ArrayOfLevel>
</HighScoreData>

For array of objects XML whould be lke this:

<HighScoreData>
    <Player>
        <PlayerName>Rocco</PlayerName>
        <Score>100</Score>
        <Level>1</Level>
    </Player>
    <Player>
        <PlayerName>Shawn</PlayerName>
        <Score>200</Score>
        <Level>2</Level>
    </Player>
    <Player>
        <PlayerName>Derrick</PlayerName>
        <Score>300</Score>
        <Level>3</Level>
    </Player>
</HighScoreData>

Comments

-1

As follows:

   <xml>
   <HighScoreData>
      <PlanerName>Player 1</PlayerName>
      <Score>200</Score>
      <Level>1</Level>
   </HighScoreData>

   <HighScoreData>
      <PlanerName>Player 2</PlayerName>
      <Score>100</Score>
      <Level>1</Level>
   </HighScoreData>

...
</xml>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.