First, I should state that this post refers to a Silverlight 4 application. Silverlight 4 seems to have a cut-down version of the Serialization model.
I have an XML file in the following format:
I read the XML into an XDocument and then call the Load(...) member on my TeamTable Class:Code:<?xml version="1.0" standalone="yes"?> <TeamTable> <TeamRow Id="1" Name="Arsenal" /> <TeamRow Id="2" Name="Aston Villa" /> <TeamRow Id="3" Name="Blackburn Rovers" /> <TeamRow Id="4" Name="Bolton Wanderers" /> <TeamRow Id="5" Name="Chelsea" /> ... </TeamTable>
For each of the TeamRow elements in the document, this function calls the Load(...) member of my TeamRow class:Code:public class TeamTable : List<TeamRow> { public void Load(XDocument document) { foreach (XElement element in document.Descendants("TeamRow")) this.Add(TeamRow.Load(element)); } }
All appears to be well, the first element passed to this function containsCode:[DataContract] public class TeamRow { [DataMember] public Int32 Id { get; set; } [DataMember] public String Name { get; set; } public TeamRow() { } public static TeamRow Load(XElement element) { DataContractSerializer serializer = new DataContractSerializer(typeof(TeamRow)); using (XmlReader reader = element.CreateReader()) { return serializer.ReadObject(reader) as TeamRow; } } }
<TeamRow Id="1" Name="Arsenal" />
as I would expect.
However the ReadObject(...) function is throwing the following exception.
This is my first attempt at this type of serialization. Can anyone help me fix the exception?Code:Expecting element 'TeamRow' from namespace 'http://schemas.datacontract.org/2004/07/...'.. Encountered 'Element' with name 'TeamRow', namespace".




Reply With Quote
