2

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 Departements, 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);
    }
}
Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
AHB
  • 43
  • 3

1 Answers1

3

The XML Namespaces specification in section 6.1 states:

The scope of a namespace declaration declaring a prefix extends from the beginning of the start-tag in which it appears to the end of the corresponding end-tag, excluding the scope of any inner declarations with the same NSAttName part. In the case of an empty tag, the scope is the tag itself.

Such a namespace declaration applies to all element and attribute names within its scope whose prefix matches that specified in the declaration.

Therefore, the data structure is serialized without repeated xmlns="http://tempuri.org/" attributes on the nested firstName and lastName elements as they would be superfluous. The reason why id and each Student elements have their own xmlns atribute is that they are siblings, forming disjoint namespace declaration scopes.

In other words, the XML produced by your code is correct and as expected. If there were extra xmlns attributes on the nested elements, it would have no semantical effect. I would be more concerned about the fact that that you are not serializing a specific root class but a plain Departement[] (unless it's just for debugging / experimenting purposes).

Community
  • 1
  • 1
Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
  • Ok thanks. But what if i need the namespace to be repeated? Who can i do that? – AHB Jan 26 '18 at 17:36
  • Why would you need it? It's not necessary. – Ondrej Tucny Jan 26 '18 at 17:38
  • I'm trying to call a method of a web service that have complex object parameter using WebClient. I serialize my object (that have the same structure of the parameter) and deserialize it as the type of the parametre, the elements of the xmlDoc that don't have the specific namespace, does not appear in the new object. – AHB Jan 26 '18 at 17:49
  • Then post a separate question describing this exact problem showing the specific code. Most likely the problem is elsewhere, not in the fact that the nested elements have no `xmlns` attributes which would be superfluous anyway. – Ondrej Tucny Jan 26 '18 at 17:53
  • I was wrong, the namespace does not affect the deserialization. Perhaps i should get uninitialized instances of nested objects? But that's an other problem. – AHB Jan 26 '18 at 18:09
  • I will do that monday, because the source code is in VM and i have to write it all from zero here. – AHB Jan 26 '18 at 18:11