XmlSerializer: Ignore properties in base class

  • Thread starter Thread starter Brecht Yperman
  • Start date Start date
B

Brecht Yperman

Hi,

I've got this class which used to be xmlserializable. Now I've made it
inherit from a base class, to which' sourcecode I have no access. When I try
to serialize this class, it gives errors since several properties in the
base class are not serializable. What is the easiest way to make the
serializer ignore the properties in the base class (in my example:
Document), preferrably without changing to much in other classes (than - in
my example - Script).

e.g.:

[XmlRoot("project")]
public class Project
{
[XmlElement("script")]
public Script [] Scripts
{
get; set;
}
}

public class Script : Document
{
[XmlAttribute("version")]
public int Version { get; set; }

[XmlElement("filter")]
public Filter [] Filters { get; set; }
}

public class Filter
{
[XmlAttribute("regex")]
public string Regex { get; set; }
}
 
Here's how I solved it, you could probably add some overloaded cast-stuff to
make it more readable.

public class ScriptData
{
[XmlAttribute("version")]
public int Version { get; set; }

[XmlElement("filter")]
public Filter [] Filters { get; set; }
}
[XmlRoot("project")]
public class Project
{ [XmlIgnore]
public Script [] Scripts
{
get; set;
}

public ScriptData[] Scripts
{
get
{
ScriptData [] ret = new ScriptData[scripts.Count];
for (int i = 0; i < ret.Count; i++)
{
ScriptData sd = new ScriptData();
Script s = scripts as Script;
sd.Version = s.Version;
sd.Filters = s.Filters;
ret = sd;
}
return sd;
}
set
{
...
}
}
}

public class Script : Document
{
[XmlAttribute("version")]
public int Version { get; set; }

[XmlElement("filter")]
public Filter [] Filters { get; set; }
}

public class Filter
{
[XmlAttribute("regex")]
public string Regex { get; set; }
}
 
Back
Top