I've include a deserializer I've written which will desieralize xml, binary
or soap into an object. And at the bottom I included example class
definitions that I think would work with the xml example you gave (maybe a
tweak or two required). I use this method with good results.
Deserialize
/// <summary>
///
/// </summary>
public class SeriaizerTypes
{
/// <summary>
///
/// </summary>
public enum enFormatter
{
/// <summary>
///
/// </summary>
fBinary,
/// <summary>
///
/// </summary>
fSoap,
/// <summary>
///
/// </summary>
fXML
}
/// <summary>
///
/// </summary>
public enum enOutput
{
/// <summary>
///
/// </summary>
oBinary,
/// <summary>
///
/// </summary>
oText
}
}
public class Deserializer<T>
{
/// <summary>
///
/// </summary>
public Deserializer()
{
}
/// <summary>
///
/// </summary>
/// <param name="pStream"></param>
/// <param name="eFormatter"></param>
/// <returns></returns>
public T Deserialize(System.IO.MemoryStream pStream,
SeriaizerTypes.enFormatter eFormatter)
{
switch (eFormatter)
{
case SeriaizerTypes.enFormatter.fBinary:
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
pBinFormatter =
new
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
return (T)pBinFormatter.Deserialize(pStream);
case SeriaizerTypes.enFormatter.fSoap:
System.Runtime.Serialization.Formatters.Soap.SoapFormatter
pSoapFormatter =
new
System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
return (T)pSoapFormatter.Deserialize(pStream);
case SeriaizerTypes.enFormatter.fXML:
System.Xml.Serialization.XmlSerializer pXmlSerializer =
new System.Xml.Serialization.XmlSerializer(typeof(T));
return (T)pXmlSerializer.Deserialize(pStream);
default:
throw new Exception(eFormatter.ToString() + " not
supported.");
}
}
public T Deserialize(System.Xml.XmlReader pReader)
{
System.Xml.Serialization.XmlSerializer pXmlSerializer = new
System.Xml.Serialization.XmlSerializer(typeof(T));
return (T)pXmlSerializer.Deserialize(pReader);
}
}
Serialize:
using System;
/// <summary>
/// Represents a list of Stores
/// </summary>
[Serializable]
[System.Xml.Serialization.XmlRoot("stores", Namespace =
"
http://mycompany.com")]
public class stores : System.Collections.Generic.List<store>
{
}
/// <summary>
/// Represents a list of Managers
/// </summary>
[Serializable]
[System.Xml.Serialization.XmlRoot("managers", Namespace =
"
http://mycompany.com")]
public class managers : System.Collections.Generic.List<manager>
{
}
/// <summary>
/// Represents a list of Administrators
/// </summary>
[Serializable]
[System.Xml.Serialization.XmlRoot("administrators", Namespace =
"
http://mycompany.com")]
public class administrators : System.Collections.Generic.List<administrator>
{
}
/// <summary>
/// Represents a list of Representatives
/// </summary>
[Serializable]
[System.Xml.Serialization.XmlRoot("representatives", Namespace =
"
http://mycompany.com")]
public class representatives :
System.Collections.Generic.List<representative>
{
}
/// <summary>
/// Represents a list of Persons
/// </summary>
[Serializable]
[System.Xml.Serialization.XmlRoot("persons", Namespace =
"
http://mycompany.com")]
public class persons : System.Collections.Generic.List<person>
{
}
/// <summary>
/// Represents a single manager
/// </summary>
[Serializable]
[System.Xml.Serialization.XmlRoot("manager", Namespace =
"
http://mycompany.com")]
public class manager
{
private int iID_m;
/// <summary>
/// ID Value
/// </summary>
[System.Xml.Serialization.XmlAttribute]
public int id
{
get
{
return iID_m;
}
set
{
iID_m = value;
}
}
/// <summary>
/// Default constructor
/// </summary>
public manager()
{
}
}
/// <summary>
/// Represents a single administrator
/// </summary>
[Serializable]
[System.Xml.Serialization.XmlRoot("administrator", Namespace =
"
http://mycompany.com")]
public class administrator : manager
{
}
/// <summary>
/// Represents a single representative
/// </summary>
[Serializable]
[System.Xml.Serialization.XmlRoot("representative", Namespace =
"
http://mycompany.com")]
public class representative : manager
{
}
/// <summary>
/// Represents a single person
/// </summary>
[Serializable]
[System.Xml.Serialization.XmlRoot("person", Namespace =
"
http://mycompany.com")]
public class person : manager
{
private string strFirstName_m;
private string strLastName_m;
/// <summary>
/// ID Value
/// </summary>
[System.Xml.Serialization.XmlAttribute]
public string firstName
{
get
{
return strFirstName_m;
}
set
{
strFirstName_m = value;
}
}
/// <summary>
/// ID Value
/// </summary>
[System.Xml.Serialization.XmlAttribute]
public string lastName
{
get
{
return strLastName_m;
}
set
{
strLastName_m = value;
}
}
}
/// <summary>
/// Represents a single store
/// </summary>
[Serializable]
[System.Xml.Serialization.XmlRoot("store", Namespace =
"
http://mycompany.com")]
public class store
{
private string strName_m;
private managers lstManagers_m;
private administrators lstAdministrators_m;
private representatives lstRepresentatives_m;
private persons lstPersons_m;
/// <summary>
/// ID Value
/// </summary>
[System.Xml.Serialization.XmlAttribute]
public string name
{
get
{
return strName_m;
}
set
{
strName_m = value;
}
}
private managers lstManagers_m;
private administrators lstAdministrators_m;
private representatives lstRepresentatives_m;
private persons lstPersons_m;
/// <summary>
///
/// </summary>
[System.Xml.Serialization.XmlArray]
public managers managers
{
get
{
return lstManagers_m;
}
set
{
lstManagers_m = value;
}
}
/// <summary>
///
/// </summary>
[System.Xml.Serialization.XmlArray]
public administrators administrators
{
get
{
return lstAdministrators_m;
}
set
{
lstAdministrators_m = value;
}
}
/// <summary>
///
/// </summary>
[System.Xml.Serialization.XmlArray]
public representatives representatives
{
get
{
return lstRepresentatives_m;
}
set
{
lstRepresentatives_m = value;
}
}
/// <summary>
///
/// </summary>
[System.Xml.Serialization.XmlArray]
public persons persons
{
get
{
return lstPersons_m;
}
set
{
lstPersons_m = value;
}
}
/// <summary>
/// Default constructor
/// </summary>
public store()
{
}
}