0

ACTUAL code and xml file

Code of program

class Program
{
    static void Main(string[] args)
    {
        string path = AppDomain.CurrentDomain.BaseDirectory + "file.xml";
        //string path2 = @"F:\fd.xml";
        FileStream fs = new FileStream(path, FileMode.Open);
XmlReader reader = XmlReader.Create(fs);
        SaveGame sav = new XmlSerializer(typeof(SaveGame)).Deserialize(reader) as SaveGame;
        Console.WriteLine(sav.player.friendshipData[0].key);
        Console.WriteLine(sav.player.friendshipData[0].value.Points);
        fs.Close();
        Console.ReadKey();
    }
}

public class SaveGame
{
    public Player player { get; set; }
}

public class Player
{
    public item[] friendshipData { get; set; }
}

public class item
{
    public string key { get; set; }
    public Friendship value { get; set; }
}

public class Friendship
{
    public int Points {get;set;}
}
}

XML File to work with:

<SaveGame>
    <player>
        <friendshipData>
            <item>
                <key>
                    <string>Name1</string>
                </key>
                <value>
                    <Friendship>
                        <Points>324</Points>
                    </Friendship>
                </value>
            </item>
            <item>
                <key>
                    <string>Name2</string>
                </key>
                <value>
                    <Friendship>
                        <Points>98</Points>
                    </Friendship>
                </value>
            </item>
        </friendshipData>
    </player>
</SaveGame>

I tried other posts, and that's not working, cause all readen values are null.

Please, how to deserialize this document? And with explanation, please.

If i set {get;set;} to variables, it won't read next item, if i set {get;} it read every item, but every item have null value.

Just in case, i can't edit XML File for some reasons. XML File is alright.

2 Answers2

0

Your data-structure doesn´t really fit your xml well. If you have a simple data-type such as int or string you can serialize and de-serialize directly into an xml-node. If you have some more complex data-structure like your Firnedship-node you need a nested node.

Having said this your data-structure should be similar to thie following:

public class SaveGame
{
    public Player player { get; set; }
}

public class Player
{
    public item[] friendshipData { get; set; }
}

public class item
{
    public Key key { get; set; }
    public Friendship value { get; set; }
}

// add this class with a single string-field
public class Key
{
    public string @string { get; set;  }
}

public class Friendship
{
    public int Points { get; set; }
}

As an aside consider following naming-conventions, which is giving classes and members of those classes PascaleCase-names, e.g. FriendshipData, Item, and Key. This however assumes you have some mapping from those names to your names within the xml. This can be done by using XmlElementAttribute:

public class Player
{
    [XmlElement("friendshipData ")] // see here how to change the name within the xml
    public item[] FriendshipData { get; set; }
}
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
0

Since you only need two values from Xml I would not use serialization. You can get a dictionary with one linq instruction.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication51
{

    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";

        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            Dictionary<string, int> players = doc.Descendants("item")
                .GroupBy(x => (string)x.Descendants("string").FirstOrDefault(), y => (int)y.Descendants("Points").FirstOrDefault())
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

        }
    }


}
jdweng
  • 33,250
  • 2
  • 15
  • 20