0
因此,在反序列化嵌套在其中的XML的XML時遇到問題。在嵌套集合(Grades)被添加之前,我能夠反序列化它,但之後一直未能。任何幫助將是偉大的!在C#中反序列化XML嵌套集合
下面是什麼XML序列化爲:
<?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>[email protected]</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>[email protected]</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>
這裏是代碼爲玩家類:
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"); }
}
}
這是序列化和反序列化的代碼:
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();
}
另外這裏是該系列的代碼:
[XmlRootAttribute("ListOfPlayers")]
public class ListOfPlayer : ObservableCollection<Player>
{
}
[XmlRoot("GradesList")]
public class ListOfGrades : List<Grades>
{
}
而且等級是一個枚舉:再次
public enum Grades
{
PreK,
K,
First,
Second,
Third,
Fourth,
Fifth,
Sixth,
Seventh,
Eighth
}
謝謝!對不起所有的信息。
保持ListOfGrades相同的玩家類屬性名的XMLRoot標籤。它會工作。目前,ListOfGrades正在查找根節點「GradesList」,該節點不存在但「GradesCurrentlyEnrolled」存在,它是屬性的名稱。 避免給XMlRoot ListOfGrades,它可能會解決問題 – Akanksha
看來,無論ListOfGrades的XMLRoot是什麼,它仍然給我一個XamlParseException當我嘗試和反序列化。當我通過它時總是打破: testPlayer =(Player)serializer.Deserialize(reader); – user2914282