Xml Serialization - deserialize a List problem
-
Does anybody know a way that I can perform the deserialization of the xml into a List that doesn't require me to create a class that inherits List<> and overrides the serialization? My current predicament is that I have the following Xml doc: 74 Table074
41 Table041
And I have the following class: public class WorkItem { #region Member variables private int _totalItems; private List _tables; private Guid _userId; private MembershipUser _user; #endregion public MembershipUser User { get { return _user; } } public int TotalItems { get { return _totalItems; } } public List Tables { get { return _tables; } } } As you can see the class doesn't match the Xml doc especially the User property, ideally I want to get the userId from the Xml doc and then query the Membership provider to return me the actual MembershipUser object. I have tried using IXmlSerializable, the ReadXml specifically is: public void ReadXml(System.Xml.XmlReader reader) { reader.MoveToElement(); reader.MoveToFirstAttribute(); _userId = new Guid(reader.Value); reader.ReadToFollowing("TotalItems"); // Try and get the total items if (!int.TryParse(reader.ReadElementString("TotalItems"), out _totalItems)) throw new SerializationException("Unable to de-serialize WorkItem due to problem with TotalItems element"); // Now we have read the specific items for the WorkItem de-serialize the tables XmlSerializer serializer = new XmlSerializer(typeof(List ), new XmlRootAttribute("Tables")); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); _tables = (List )serializer.Deserialize(reader); } Which works fine for a single WorkItem element but when I a
-
Does anybody know a way that I can perform the deserialization of the xml into a List that doesn't require me to create a class that inherits List<> and overrides the serialization? My current predicament is that I have the following Xml doc: 74 Table074
41 Table041
And I have the following class: public class WorkItem { #region Member variables private int _totalItems; private List _tables; private Guid _userId; private MembershipUser _user; #endregion public MembershipUser User { get { return _user; } } public int TotalItems { get { return _totalItems; } } public List Tables { get { return _tables; } } } As you can see the class doesn't match the Xml doc especially the User property, ideally I want to get the userId from the Xml doc and then query the Membership provider to return me the actual MembershipUser object. I have tried using IXmlSerializable, the ReadXml specifically is: public void ReadXml(System.Xml.XmlReader reader) { reader.MoveToElement(); reader.MoveToFirstAttribute(); _userId = new Guid(reader.Value); reader.ReadToFollowing("TotalItems"); // Try and get the total items if (!int.TryParse(reader.ReadElementString("TotalItems"), out _totalItems)) throw new SerializationException("Unable to de-serialize WorkItem due to problem with TotalItems element"); // Now we have read the specific items for the WorkItem de-serialize the tables XmlSerializer serializer = new XmlSerializer(typeof(List ), new XmlRootAttribute("Tables")); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); _tables = (List )serializer.Deserialize(reader); } Which works fine for a single WorkItem element but when I a
When you have a List of WorkItems, you have to call the Deserialize (which inturn calls the ReadXML) multiple times to load the complete list. You will have to iterate thru the XML Reader (thru the list of WorkItem elements serialized into a single XML file) and call the Deserialize for each WorkItem element, and add the retrived WorkItem to a list. With this logic you can finally populate the List of WorkItems loaded back from XML, into which you have previously serialized the list of WorkItems. You can try this - TextReader reader = new StringReader(xmlData); System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create( reader ); List<WorkItem> workItemList = new List<WorkItem>( ); try { xmlReader.ReadStartElement( ); while ( ( xmlReader.MoveToContent( ) == XmlNodeType.Element && xmlReader.LocalName == typeof( WorkItem ).Name ) ) { workItemList.Add( ( WorkItem )new XmlSerializer( typeof( WorkItem ) ).Deserialize( xmlReader ) ); xmlReader.Read( ); } } catch (Exception ex) { } finally { xmlReader.Close(); Reader.Close(); Reader.Dispose(); }