XmlSerializer + Abstract Class + Derived Classes = Useless Namespaces

My first question is about SO :,) and about the XmlSerializer and namespace issues.

I know that there are already many questions about how to remove the default Xml namespace from the root element of an Xml file, and that is not the subject.

My question is how to remove it from child nodes when using derived classes?

I created my own serializer that can create custom namespaces or just ignore them, and it works well for the root element.

But when I use an abstract class to list some derived classes inside the List of attributes of the insert serialization 2 inside the nodes of each derived class.

Like this:

<root> <elements> <element p3:type="XmlDerivedClass" xmlns:p3="{schema_url}" > </element> </elements> </root> 

As for my classes:

 // Root element [XmlRoot("root", Namespace="")] public class XmlRootElement { List<XmlBaseClass> _Elements; } // Base class [XmlInclude(typeof(XmlDerivedClass))] // Mandatory, prevents serialization errors [XmlRoot(Namespace="")] public abstract class XmlBaseClass // Derived class [XmlRoot("element", Namespace="")] public class XmlDerivedClass : XmlBaseClass 

I tried some general solutions:

  • Using the Namespace = "" Attribute
  • Implementing the XmlNamespaceDeclarations property (with the correct empty namespace)
  • Moving XmlRoot () from the base classes to the derived ones.
  • Changing XmlRoot () to XmlElement ()

I will try to add the XmlInclude tag to the List to see that it will change something.

So far, nothing has been able to eliminate these damned namespaces ...

If anyone has a solution for this, I will be happy to try it.

[EDIT 02/21/2014] I seem to be the only one who has encountered this problem. I use simple string.Replace to remove useless XML, but it's pretty messy.


PS: For context, tags are not a problem for the parser at the other end, but they are not needed, so I'm looking for a way to remove them.

PS2: Sorry for any spelling mistakes, English is not my native language.

+7
c # xml serialization xml-namespaces
source share
2 answers

I am not sure if this is a solution that satisfies your needs because it is not very pleasant, but it works and it does not seem too dirty.

 public abstract class Abs { public int Data { get; set; } } public class A : Abs{} public class B : Abs{} [Serializable] [XmlRoot(elementName: "name")] public class Ser { [XmlElement(elementName: "A")] public List<A> AList { get; set; } [XmlElement(elementName: "B")] public List<B> BList { get; set; } [XmlIgnore] public List<Abs> AbsList { get { var list = new List<Abs>(AList.ConvertAll(x=>(Abs)x)); list.AddRange(BList.ConvertAll(x=>(Abs)x)); return list; } } } 

Instead of XmlInclude, you can create a list with objects of a derived class, and then add them together, or possibly cache them in a private member. Please note that this is still far from ideal, but it works for me, so I would like to share it.

+2
source share

If you add [XmlType] , you can provide the serializer with type information, for example, to the namespace:

 // Base class [XmlInclude(typeof(XmlDerivedClass))] // Mandatory, prevents serialization errors [XmlType(Namespace="")] public abstract class XmlBaseClass // Derived class [XmlType("element", Namespace="")] public class XmlDerivedClass : XmlBaseClass 
0
source share

All Articles