CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2003
    Posts
    18

    Question Why does deserialization fail?

    XML:
    Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <TTTTModel>
        <TTTTClass>
            <TTTTType>Deployment</TTTTType>
            <TTTTDate>2009-09-30T00:00:00-07:00</TTTTDate>
        </TTTTClass>
        <TTTTClass>
            <TTTTType>Deployment</TTTTType>
            <TTTTDate>2009-09-20T00:00:00-07:00</TTTTDate>
        </TTTTClass>
        <TTTTClass>
            <TTTTType>Copy</TTTTType>
            <TTTTDate>2009-02-23T00:00:00-07:00</TTTTDate>
        </TTTTClass>
    </TTTTModel>

    The code:

    Code:
    [Serializable]
        public class ActionClass
        {
            [XmlElement(Form=XmlSchemaForm.Unqualified)]
            public string ActionType { get; set; }
            [XmlElement(Form = XmlSchemaForm.Unqualified)]
            public DateTime ActionDate { get; set; }
        }
    
    
    
    
        public class ActionModel : List<ActionClass>
        {
            public ActionModel()
            {
                XmlSerializer xs = new XmlSerializer(typeof(ActionModel), "");
    
                using (Stream ms = new FileStream("Demo.xml", FileMode.Open, FileAccess.Read))
                {
                    object inp = xs.Deserialize(ms);
                    this.AddRange((ActionModel)inp);
                }
            }
        }

    Why I get exception on object inp = xs.Deserialize(ms)?


    <ActionModel xmlns=''> was not expected.

    How to fix it?
    Last edited by Arjay; November 27th, 2009 at 07:48 PM.

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

    Re: Why does deserialization fail?

    Code:
    ///<summary>
    /// Request adapter
    ///</summary>
    [XmlType( AnonymousType = true )]
    [XmlRoot( ElementName = "TTTTModel", Namespace = "", IsNullable = false )]
    publicclassActionModel
    {
      ///<remarks/>
      [XmlElement( "TTTTClass" )]
      publicActionClass [ ] Actions
      {
        get { return _actionList.ToArray( ); }
        set
        {
          if ( null == _actionList )
          {
             _actionList = newList<ActionClass>( );
          }
     
          if ( value != null )
          {
            _actionList.AddRange( value );
          }
        }
      } 
    
     
      privateList<ActionClass> _actionList = newList<ActionClass>( );
    }
     
    [Serializable( )]
    [XmlType( AnonymousType = true )]
    publicclassActionClass
    {
      [XmlElement( "TTTTType" )]
      publicstring Type { get { return _type; } set { _type = value; } }
     
      [XmlElement( "TTTTDate" )]
      publicstring Date { get { return _date; } set { _date = value; } }
    
     
      privatestring _type;
      privatestring _date;
    }
    
     
    classProgram
    {
      staticvoid Main( string [ ] args )
      {
        XmlSerializer xs = newXmlSerializer( typeof( ActionModel ), "" );
     
        using ( Stream ms = newFileStream( "Demo.xml", FileMode.Open, FileAccess.Read ) )
        {
          ActionModel am = ( ActionModel )xs.Deserialize( ms );
        }
      }
    }

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