Click to See Complete Forum and Search --> : Why does deserialization fail?


senglory
November 21st, 2009, 01:14 AM
XML:

<?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:


[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?

Arjay
November 27th, 2009, 07:08 PM
/// <summary>
/// Request adapter
/// </summary>
[XmlType( AnonymousType = true )]
[XmlRoot( ElementName = "TTTTModel", Namespace = "", IsNullable = false )]
public class ActionModel
{
/// <remarks/>
[XmlElement( "TTTTClass" )]
public ActionClass [ ] Actions
{
get { return _actionList.ToArray( ); }
set
{
if ( null == _actionList )
{
_actionList = new List<ActionClass>( );
}

if ( value != null )
{
_actionList.AddRange( value );
}
}
}


private List<ActionClass> _actionList = new List<ActionClass>( );
}

[Serializable( )]
[XmlType( AnonymousType = true )]
public class ActionClass
{
[XmlElement( "TTTTType" )]
public string Type { get { return _type; } set { _type = value; } }

[XmlElement( "TTTTDate" )]
public string Date { get { return _date; } set { _date = value; } }


private string _type;
private string _date;
}


class Program
{
static void Main( string [ ] args )
{
XmlSerializer xs = new XmlSerializer( typeof( ActionModel ), "" );

using ( Stream ms = new FileStream( "Demo.xml", FileMode.Open, FileAccess.Read ) )
{
ActionModel am = ( ActionModel )xs.Deserialize( ms );
}
}
}