CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Threaded View

  1. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Serialization from Xml into Object

    Does Silverlight support the XmlSerializer class?

    If so, create a couple of classes with Xml attributes:

    Code:
    namespace CG
    {
      #region Using Directives
      using System;
      using System.Collections.Generic;
      using System.Xml;
      using System.Xml.Serialization;
      #endregion Using Directives
     
      ///<summary>
      /// Serialization class used to read the teamtable
      ///</summary>
      [Serializable]
      [XmlType( AnonymousType = true )]
      [XmlRoot( ElementName = "TeamTable", Namespace = "", IsNullable = false )]
      publicclassTeams
      {
        ///<summary/>
        publicstaticTeams Open( string configFile )
        {
          var serializer = newXmlSerializer( typeof( Teams ) );
    
          using ( var reader = XmlReader.Create( configFile ) )
          {
            return ( Teams ) serializer.Deserialize( reader );
          }
        }
     
        ///<remarks/>
        [XmlElement( "TeamRow", IsNullable = false )]
        publicTeam [ ] Rows
        {
          get
          {
            return _rows.ToArray( );
          }
          set
          {
            if ( null == _rows )
            {
              _rows = newList<Team>( );
            }
            _rows.AddRange( value );
          }
        }
        
        privateList<Team> _rows = newList<Team>( );
      }
     
      ///<remarks/>
      [Serializable]
      [XmlType( AnonymousType = true )]
      publicclassTeam
      {
        ///<remarks/>
        [XmlAttribute( AttributeName = "Id" )]
        publicint Id { get { return _id; } set { _id = value; } }
     
        ///<remarks/>
        [XmlAttribute( AttributeName = "Name" )]
        publicstring Name { get { return _name; } set { _name = value; } }
    
    
        privateint _id;
        privatestring _name;
      }
    }


    Then loading up the xml is:

    Code:
    var teamList = newList<Team>( );
     
    // Loads the xml into a Teams instance
    var teams = Teams.Open("teams.xml");  
     
    // Retrieve the TeamRows
    teamList.AddRange( teams.Rows );



    Attached Files Attached Files

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured