• Advertisement
Sign in to follow this  

How to apply XmlArray and XmlArrayItem in C#

This topic is 4664 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was told to lookup XmlArrayItemAttribute and XmlArrayAttribute. WHERE would I apply these attributes in the following code? Do these attributes work on an ArrayList, or what? I am very confused. Here is the code. Keep in mind that I actually do NOT understand XML Serialization that well!!! The XML attributes you see were written by somebody else...and then that person stopped helping me and now I am stuck. I hope someone can help me. The ENTIRE game source is located here. Just click on the GAME SOURCE DOWNLOAD link
using System;
using System.Collections;
using System.Runtime.Serialization;
using System.Xml.Serialization;

namespace Robotz
{
	 [XmlRoot("GamePlayer"), Serializable]
	public class GameMove
	{
		private int _fromX;
		private int _fromY;
		private int _toX;
		private int _toY;
		private int _prevMovementPoints;

		public GameMove()
		{
		}


		[XmlAttribute("PrevMovePoints")]
		public int PrevMovePoints
		{
			get{return _prevMovementPoints;}
		}

		[XmlAttribute("FromX")]
		public int FromX
		{
			get{return _fromX;}
		}

		[XmlAttribute("FromY")]
		public int FromY
		{
			get{return _fromY;}
		}

		[XmlAttribute("ToX")]
		public int ToX
		{
			get{return _toX;}
		}

		[XmlAttribute("ToY")]
		public int ToY
		{
			get{return _toY;}
		}

		public GameMove(int fromX,int fromY,int toX,int toY, int prevMovementPoints)
		{
			_fromX = fromX;
			_fromY = fromY;
			_toX = toX;
			_toY = toY;
			_prevMovementPoints = prevMovementPoints;
		}

		public void PlayMove(GameWorld gw)
		{
			gw.MovePiece(_fromX,_fromY,_toX,_toY);
		}

	}


	[XmlRoot("GameTurn"), Serializable]
	public class GameTurn
	{
		private ArrayList _moves;

		[XmlElement("move")]		
		public Robotz.GameMove[] GameMove
		{
			get
			{
				Robotz.GameMove[] GameMove = new Robotz.GameMove[_moves.Count];
				_moves.CopyTo(GameMove);
				return GameMove;
			}
			set
			{
				if(value == null) return;
				Robotz.GameMove[] movea = (Robotz.GameMove[])value;
				_moves.Clear();
				foreach(Robotz.GameMove gm in movea)
					_moves.Add(gm);
			}
		}
		
		
		
		public ArrayList Moves
		{
			get{return _moves;}
		}
		
		public GameTurn()
		{
			_moves = new ArrayList();
		}

		public void AddMove(GameMove gm)
		{
			_moves.Add(gm);
		}

		public int DeleteLastMove()  //Returns previous movement points for last move made
		{
			int prevMovementPoint = 0;
			if (_moves.Count > 0)
			{
				prevMovementPoint = ((GameMove)_moves[_moves.Count - 1]).PrevMovePoints;
				_moves.RemoveAt(_moves.Count - 1);
			}
			return prevMovementPoint;
		}

		public bool CanUndo
		{
			get {return _moves.Count > 0;}
		}


		public void PlayTurn(GameWorld gw)
		{
			foreach (GameMove gm in _moves)
			{
				gm.PlayMove(gw);
			}
		}
	}

	[XmlRoot("GameStore"), Serializable]
	public class GameStore
	{
		private GamePlayer _playerOne;
		private GamePlayer _playerTwo;
		private ArrayList _gameTurns;

		public GameStore()
		{
		}

		[XmlElement("turn")]		
		public Robotz.GameTurn[] GameTurn
		{
			get
			{
				Robotz.GameTurn[] GameTurn = new Robotz.GameTurn[_gameTurns.Count];
				_gameTurns.CopyTo(GameTurn);
				return GameTurn;
			}
			set
			{
				if(value == null) return;
				Robotz.GameTurn[] turnsa = (Robotz.GameTurn[])value;
				_gameTurns.Clear();
				foreach(Robotz.GameTurn ta in turnsa)
					_gameTurns.Add(ta);
			}
		}
		
		
		public ArrayList Turns
		{
			get{return _gameTurns;}
		}

		[XmlAttribute("GameTurnCount")]
		public int TurnCount 
		{
			get{return _gameTurns.Count;}
		}

		[XmlAttribute("GameTurnPlayerOne")]
		public GamePlayer GetPlayerOne
		{
			get{return _playerOne;}
		}

		[XmlAttribute("GameTurnPlayerTwo")]
		public GamePlayer GetPlayerTwo
		{
			get{return _playerTwo;}
		}

		public GamePlayer GetCurrentPlayer
		{
			get
			{
				if((_gameTurns.Count % 2) == 0)
				{
					return GetPlayerOne;
				}
				else
				{
					return GetPlayerTwo;
				}
			}
		}

		public GameStore(GamePlayer playerOne, GamePlayer playerTwo)
		{
			_playerOne = playerOne;
			_playerTwo = playerTwo;
			_gameTurns = new ArrayList();
		}

		public void PlayAllTurns(GameWorld gw)
		{
			gw.InitGamePieceArray();
			gw.PointsLeft = 5; //starting points
			foreach (GameTurn gt in _gameTurns)
			{
				gt.PlayTurn(gw);
				gw.PointsLeft = 10; //Next turn points available
			}
		}

		public void AddTurn(GameTurn gt)
		{
			_gameTurns.Add(gt);
		}

		public bool CanReplay
		{
			get{return (_gameTurns.Count > 0);}
		}
	}	
}


Share this post


Link to post
Share on other sites
Advertisement
Here is the resulting XML file:

<?xml version="1.0" encoding="utf-8" ?>
- <GameStore xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <turn>
<move />
- <Moves>
<anyType xsi:type="GameMove" />
</Moves>
</turn>
- <turn>
<move />
- <Moves>
<anyType xsi:type="GameMove" />
</Moves>
</turn>
- <Turns>
- <anyType xsi:type="GameTurn">
<move />
- <Moves>
<anyType xsi:type="GameMove" />
</Moves>
</anyType>
- <anyType xsi:type="GameTurn">
<move />
- <Moves>
<anyType xsi:type="GameMove" />
</Moves>
</anyType>
</Turns>
</GameStore>

Share this post


Link to post
Share on other sites
I can't help with this, but if you're doing XML stuff in C# you should check out...

http://research.microsoft.com/Comega/

I've found it very useful.

Share this post


Link to post
Share on other sites
Quote:
Original post by boebi
I can't help with this, but if you're doing XML stuff in C# you should check out...

http://research.microsoft.com/Comega/

I've found it very useful.



I'll check it out!!

Thanks!

Tom

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement