.net XmlSerializer, ignore base class properties

Suppose we have a split class "SerializableLabel" from the base class "System.Windows.Controls.

[XmlRoot("SerializableLabel")]
public class SerializableLabel : Label
{
    public string foo = "bar";
}

I would like to serialize this class, but ignore ALL properties in the parent class. Ideally, xml would look something like this:

<SerializableLable>
    <foo>bar</foo>
</SerializableLable>

How is this achieved?

My first attempt used the typical XmlSerializer approach:

XmlSerializer s = new XmlSerializer(typeof(SerializableLabel));
TextWriter w = new StreamWriter("test.xml");
s.Serialize(w, lbl);
w.Close();

But this throws an exception, because the serializer tries to serialize the property of the base class, which is the interface (ICommand command).

+3
source share
4 answers

( , JP) , . , , . , , .

, SerializableLabel, () , , () .

+1

, Xml.
. .

+1

IXmlSerializable, , . , - BackColor SerializableLabel, .

0

text.xml SerializableLabel, SerializableLable ( )

0

Source: https://habr.com/ru/post/1711956/


All Articles