L
Lee Alexander
hi,
Perhaps someone can help me. I am trying to serialize an object but the
serialization isn't working. I have created a small console app to show my
problem. Perhaps someone with more knowledge could nudge me in the correct
direction.
Regards
Lee
#region Using directives
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
#endregion
namespace TestSerialization
{
public class Employee
{
public Employee()
{
}
public Employee( string name )
{
this.Name = name;
}
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
private string _name = string.Empty;
}
public class Work
{
public Work()
{
}
public EmployeeCollection Employees
{
get
{
return _employees;
}
}
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public class EmployeeCollection: ICollection<Employee>
{
#region ICollection<Employee> Members
public void Add( Employee item )
{
_employees.Add( item );
}
public void Clear()
{
_employees.Clear();
}
public bool Contains( Employee item )
{
return _employees.Contains( item );
}
public void CopyTo( Employee[] array, int arrayIndex )
{
_employees.CopyTo( array, arrayIndex );
}
public int Count
{
get { return _employees.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public bool Remove( Employee item )
{
return _employees.Remove( item );
}
#endregion
#region IEnumerable<Employee> Members
IEnumerator<Employee> IEnumerable<Employee>.GetEnumerator()
{
return _employees.GetEnumerator();
}
#endregion
private List<Employee> _employees = new List<Employee>();
}
private EmployeeCollection _employees = new EmployeeCollection();
private string _name = string.Empty;
}
class Program
{
private static string DumpException( Exception ex )
{
string info = string.Empty;
do
{
info += WriteExceptionInfo( ex );
ex = ex.InnerException;
} while( ex != null );
return info;
}
private static string WriteExceptionInfo( Exception ex )
{
string info = string.Empty;
info += string.Format( "--------- Exception Data ---------" ) +
Environment.NewLine;
info += string.Format( "Exception Type: {0}", ex.GetType().FullName ) +
Environment.NewLine;
info += string.Format( "Message: {0}", ex.Message );
return info;
}
static void Main( string[] args )
{
try
{
Work work = new Work();
work.Name = "SomePlace";
work.Employees.Add( new Employee( "Lee" ) );
work.Employees.Add( new Employee( "Stuart" ) );
XmlSerializer s = new XmlSerializer( typeof( Work ) );
s.Serialize( Console.Out, work );
}
catch( Exception e )
{
Console.WriteLine( DumpException( e ) );
}
Console.Read();
}
}
}
Perhaps someone can help me. I am trying to serialize an object but the
serialization isn't working. I have created a small console app to show my
problem. Perhaps someone with more knowledge could nudge me in the correct
direction.
Regards
Lee
#region Using directives
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
#endregion
namespace TestSerialization
{
public class Employee
{
public Employee()
{
}
public Employee( string name )
{
this.Name = name;
}
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
private string _name = string.Empty;
}
public class Work
{
public Work()
{
}
public EmployeeCollection Employees
{
get
{
return _employees;
}
}
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public class EmployeeCollection: ICollection<Employee>
{
#region ICollection<Employee> Members
public void Add( Employee item )
{
_employees.Add( item );
}
public void Clear()
{
_employees.Clear();
}
public bool Contains( Employee item )
{
return _employees.Contains( item );
}
public void CopyTo( Employee[] array, int arrayIndex )
{
_employees.CopyTo( array, arrayIndex );
}
public int Count
{
get { return _employees.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public bool Remove( Employee item )
{
return _employees.Remove( item );
}
#endregion
#region IEnumerable<Employee> Members
IEnumerator<Employee> IEnumerable<Employee>.GetEnumerator()
{
return _employees.GetEnumerator();
}
#endregion
private List<Employee> _employees = new List<Employee>();
}
private EmployeeCollection _employees = new EmployeeCollection();
private string _name = string.Empty;
}
class Program
{
private static string DumpException( Exception ex )
{
string info = string.Empty;
do
{
info += WriteExceptionInfo( ex );
ex = ex.InnerException;
} while( ex != null );
return info;
}
private static string WriteExceptionInfo( Exception ex )
{
string info = string.Empty;
info += string.Format( "--------- Exception Data ---------" ) +
Environment.NewLine;
info += string.Format( "Exception Type: {0}", ex.GetType().FullName ) +
Environment.NewLine;
info += string.Format( "Message: {0}", ex.Message );
return info;
}
static void Main( string[] args )
{
try
{
Work work = new Work();
work.Name = "SomePlace";
work.Employees.Add( new Employee( "Lee" ) );
work.Employees.Add( new Employee( "Stuart" ) );
XmlSerializer s = new XmlSerializer( typeof( Work ) );
s.Serialize( Console.Out, work );
}
catch( Exception e )
{
Console.WriteLine( DumpException( e ) );
}
Console.Read();
}
}
}