简体   繁体   中英

Deserialize XML nested collections in C#

So I am having trouble when deserializing my XML that has nested collections in it. I was able to deserialize it before the nested collection(Grades) was added but have not been able to since. Any help would be great!

Below is what the XML is serialized as:

<?xml version="1.0" encoding="utf-8"?>
<ListOfPlayers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Player>
    <FirstName>Michael</FirstName>
    <LastName>Jordan</LastName>
    <EmailAddress>jordanm@seattleu.edu</EmailAddress>
    <PhoneNumber>206-227-6000</PhoneNumber>
    <NumberOfExtraTeamsWillingToFill>2</NumberOfExtraTeamsWillingToFill>
    <IsActive>true</IsActive>
    <InactiveReason />
    <PlayerNotes />
    <GradesCurrentlyEnrolled>
      <Grades>First</Grades>
      <Grades>Second</Grades>
    </GradesCurrentlyEnrolled>
    </Player>
<Player>
    <FirstName>Magic</FirstName>
    <LastName>Johnson</LastName>
    <EmailAddress>johnsonm@seattleu.edu</EmailAddress>
    <PhoneNumber>206-227-6000</PhoneNumber>
    <NumberOfExtraTeamsWillingToFill>3</NumberOfExtraTeamsWillingToFill>
    <IsActive>true</IsActive>
    <InactiveReason />
    <PlayerNotes>Has a new baby</PlayerNotes>
    <GradesCurrentlyEnrolled>
      <Grades>Sixth</Grades>
      <Grades>Eighth</Grades>
    </GradesCurrentlyEnrolled>
    </Player>
</ListOfPlayers>

Here is the Code for Player Class:

 public class Player : ModelBase
    {
    public Player()
    {
        FirstName = "";
        LastName = "";
        PhoneNumber = "";
        EmailAddress = "";
        NumberOfExtraTeamsWillingToFill = 0;
        IsActive = true;
        InactiveReason = "";
        PlayerNotes = "";
        GradesCurrentlyEnrolled = new ListOfGrades();
    }

    public Player(string firstName, string lastName, string phoneNumber, string emailAddress, int numberOfExtraTeamsWillingToFill,
        bool isActive, string inactiveReason, ListOfGrades gradesCurrentlyEnrolled, string playerNotes)
    {
        FirstName = firstName;
        LastName = lastName;
        PhoneNumber = phoneNumber;
        EmailAddress = emailAddress;
        NumberOfExtraTeamsWillingToFill = numberOfExtraTeamsWillingToFill;
        IsActive = isActive;
        InactiveReason = inactiveReason;
        PlayerNotes = playerNotes;
        GradesCurrentlyEnrolled = gradesCurrentlyEnrolled;
    }

    // IsPlayerFloating
    public bool IsPlayerFloating
    {
        get
        {
            return (GradesCurrentlyEnrolled.Count == 0);
        }
    }

    // GradesCurrentlyEnrolled
    private ListOfGrades _gradesCurrentlyEnrolled;
    public ListOfGrades GradesCurrentlyEnrolled
    {
        get { return _gradesCurrentlyEnrolled; }
        set { _gradesCurrentlyEnrolled = value;}         //SendPropertyChanged("GradesCurrentlyEnrolled"); }
    }

}

And this is the code for Serializing and deserializing:

       private void LoadAllPlayers()
    {
        ListOfPlayer _playersForSerial;
        Player testPlayer;
        XmlSerializer serializer = new XmlSerializer(typeof(Player));

        StreamReader reader = new StreamReader(filename);
        testPlayer = (Player)serializer.Deserialize(reader);
        reader.Close();

        Players.Add(new PlayerViewModel(testPlayer));

        foreach (Player pv in _playersForSerial)
        {
            Players.Add(new PlayerViewModel());
        }

    }

    public void SaveAllPlayers()
    {
        ListOfPlayer _playersForSerial = new ListOfPlayer();

        //Creates a list of Players fromt eh ViewModels
        foreach (PlayerViewModel pv in Players)
        {
            _playersForSerial.Add(pv.GetPlayer);

        }

        // Note that only the collection is serialized -- not the 
        // CollectionName or any other public property of the class.
        XmlSerializer x = new XmlSerializer(typeof(ListOfPlayer));
        TextWriter writer = new StreamWriter(filename);
        x.Serialize(writer, _playersForSerial);
        writer.Close();
    }

Also here is the code for the collections:

[XmlRootAttribute("ListOfPlayers")]
public class ListOfPlayer : ObservableCollection<Player>
{
}

[XmlRoot("GradesList")]
public class ListOfGrades : List<Grades>
{
}

Also Grades is an enum:

public enum Grades
{
    PreK,
    K,
    First,
    Second,
    Third,
    Fourth,
    Fifth,
    Sixth,
    Seventh,
    Eighth
}

Thanks again! Sorry for all the information.

您应该像这样向xmlSerializer提供有关其他类型的信息:

XmlSerializer serializer = new XmlSerializer(typeof(Player),new []{typeof(ListOfGrades)});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM