Serialization and Xml mapping

  • Thread starter Thread starter BigStef
  • Start date Start date
B

BigStef

Hi folks,
I got this XML file the other that I wanted to deserialize to
objects that I've built.

<?xml version="1.0" encoding="utf-8" ?>
<stores>
<store name="MR. X and Miss Yid="1">
<managers>
<manager id="1" />
<manager id="2" />
</managers>
<administrators>
<administrator id="1" />
<administrator id="2" />
</administrators>
<representatives>
<representative id="3" />
<representative id="4" />
<representative id="5" />
</representatives>
<persons>
<person firstName="X" lastName="Ex" id="1" />
<person firstName="Y" lastName="Why" id="2" />
<person firstName="Z" lastName="ZeZee" id="3" />
<person firstName="D" lastName="Dee" id="4" />
<person firstName="F" lastName="Effin" id="5" />
</persons>
</store>
</stores>

As you can see, all the people are in the persons section of the xml
and they're referred by their id in the other sections. They can be
found in one or more sections as Admins, managers and/or
representatives.
I was wondering how I can achieve serialization that will list Admins,
Managers and representatives with their full informations which is
stored in the person element?

Thanks a lot!

Stef
 
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()
{
}
}
 
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()
{
}

}

Hi Jeremy,
Thanks for your answer. My problem wasn't about how to simply
serialize / deserialize informations stored in the XML "as is".
My question was to know if there's a way, when serialization occurs,
to map the person informations to the actual Representatives, managers
and admins?
The person section acts as a sort of pool of information with an id
that is referrenced in the other nodes. Like, person with id 1 has
the concrete information but this person is an admin AND a manager and
is referred to by it's id only in the admin and manager nodes.

When serializing, I want to be able to instruct my app that when
reading ID 1, go grab the concrete information in the person node
related to id 1 and so on.

I'm still searching for a way to do that so, if anyone has any
pointers on how to achieve this, I would be more than happy to look at
the solution you may have. If my searches points me in a direction
that helps me solve this, I will post the solution here.
Thanks for reading!

Stef
 
Back
Top