Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Xml Serialization - deserialize a List problem

Xml Serialization - deserialize a List problem

Scheduled Pinned Locked Moved C#
databaseregexxmljsonhelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nathan Gloyn
    wrote on last edited by
    #1

    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

    L 1 Reply Last reply
    0
    • N Nathan Gloyn

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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&lt;WorkItem&gt; workItemList = new List&lt;WorkItem&gt;( ); try { xmlReader.ReadStartElement( ); while ( ( xmlReader.MoveToContent( ) == XmlNodeType.Element &amp;&amp; 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(); }

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups