Hi,I wonder whether I might ask you a question about that book?

Jul 30, 2008 at 11:15 AM
Edited Jul 30, 2008 at 11:48 AM

public static void Save(string settingsFilename, GameSettings gameSettings)
        {
            Stream stream = File.OpenWrite(settingsFilename);
            XmlSerializer serializer = new XmlSerializer(typeof(GameSettings));

            serializer.Serialize(stream, gameSettings);
        }

I saved the xml file,but I loaded it fail.

The xml file added something like this: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"

So I added the code:

XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
            xmlns.Add(String.Empty, String.Empty);

            serializer.Serialize(stream, gameSetting,xmlns);

The xml file is:
<?xml version="1.0"?>
<GameSetting>
  <NumEnemy>10</NumEnemy>
  <Life>300</Life>
</GameSetting>Life>
</GameSetting>

Can you help me ?Thankyou very much.

PS:If I saved the life first,it loaded failed.And if I saved NumEnemy first,everything is well.
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
That code makes no difference to load the xml file or save the xml file.
And sometime the xml file will change like this.
<?xml version="1.0"?>
<GameSetting xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <NumEnemy>5</NumEnemy>
  <Life>300</Life>
</GameSetting>>
That will be fail.

Hmm..I delete the xml file,and create it,write the code every time.

Coordinator
Aug 2, 2008 at 7:25 AM
Hi. I'm not sure if I understand your problem, you trying to write and read an XML file but it fails when you read it?

Note that the order of the data in the XML file is important, thus first adding the <NumEnemy> tag and next the <Life> tag, is not the same of adding the <Life> first. And the first line on the XML file just show its version.

You can check some great tutorials about XML at Shawn's blog: http://blogs.msdn.com/shawnhar/default.aspx