The following is the class entity that will be serialized:
[XmlRoot ("Root")]
public class Entity
{
[XmlAttribute (AttributeName = "Attr1")]
public string Attribute1;
[XmlElement ("Secondlevel")]
Public Secondlevel Secondlevel;
public override string ToString ()
{
Return Serializer.serialize (this);
}
}
public class Secondlevel
{
[XmlElement ("Thirdlevel")]
Public list<thirdlevel> Thirdlevel;
}
public class Thirdlevel
{
[XmlAttribute (AttributeName = "Attribute1")]
public string Attribute1;
[XmlAttribute (AttributeName = "Attribute2")]
public string Attribute2;
[XmlElement ("Ele1")]
Public Element Ele1;
}
public class Element
{
[XmlAttribute (AttributeName = "Attr1")]
public string Attribute1;
[XmlText]
public string Value;
}
The following is an example of the serialized XML file for an entity class:
<?xml version= "1.0"?>
<root attr1= "Attribute1" >
<SecondLevel>
<thirdlevel attribute1= "AT1" attribute2= "AT2" >
<ele1 attr1= "AT1" >v</Ele1>
</ThirdLevel>
<thirdlevel attribute1= "att1" attribute2= "Att2" >
<ele1 attr1= "Att1" >va</Ele1>
</ThirdLevel>
<thirdlevel attribute1= "attr1" attribute2= "ATTR2" >
<ele1 attr1= "ATTR1" >val</Ele1>
</ThirdLevel>
</SecondLevel>
</Root>
It is worth mentioning that in practice, the custom namespace [xmlns: ...] method is to use the overloaded method of the XmlSerializer class:
public void Serialize (stream stream, object o, XmlSerializerNamespaces namespaces);
is serialized, where the "namespaces" parameter can be created by the following code:
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces (
New[] {new XmlQualifiedName (string. Empty, "")});
The use of C # XML serialization and deserialization attributes