I have a class with a nested list of another class, like this:
public class Departement {
[XmlElement (Namespace = "http://tempuri.org/")]
string Id;
[XmlElement (Namespace = "http://tempuri.org/")]
List<Student> listStident = new List<Student> ();
}
public class Student
{
[XmlElement (Namespace = "http://tempuri.org/")]
string firstName;
[XmlElement (Namespace = "http://tempuri.org/")]
string lastName;
}
When I try to serialize an array of Departement
s, I get an xml like that:
<ArrayOfDepartement xmlns="http://www.w3...">
<Departement>
<id xmlns== "http://tempuri.org/">1234567890</id>
<Student xmlns== "http://tempuri.org/">
<firstName>med</firstName>
<lastName>ali</lastName>
</Student>
<Student xmlns== "http://tempuri.org/">
<firstName>joe</firstName>
<lastName>doe</lastName>
</Student>
</Departement>
</ArrayOfDepartement>
No namespace attribute is generated for the nested elements. The code I use to serialize is:
public void Serialize(object oToSerialize)
{
XmlSerializer xmlSerializer = new XmlSerializer(oToSerialize.GetType());
XmlDocument xDoc = new XmlDocument();
using (var stream = new MemoryStream())
{
xmlSerializer.Serialize(stream, oToSerialize);
stream.Flush();
stream.Seek(0, SeekOrigin.Begin);
xDoc.Load(stream);
}
}