Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Attribute XmlIgnore and XamlWriter class - XmlIgnore not working

I have a class, containing a property Brush MyBrush marked as [XmlIgnore]. Nevertheless it is serialized in the stream causing trouble when trying to read via XamlReader.

I did some tests, e.g. when changing the visibility of the Property (to internal) it is gone in the stream. Unfortunately I cannot do this in my particular scenario.

  1. Did anybody have the same issue and?
  2. Do you see any way to work around this?

Remark: C# 4.0 as far I can tell

This is a method from my Unit Test where I do test the XamlSerialization:

            // buffer to a StringBuilder
            StringBuilder sb = new StringBuilder();
            XmlWriter writer = XmlWriter.Create(sb, settings);
            XamlDesignerSerializationManager manager = new XamlDesignerSerializationManager(writer) {XamlWriterMode = XamlWriterMode.Expression};

            XamlWriter.Save(testObject, manager);
            xml = sb.ToString();
            Assert.IsTrue(!String.IsNullOrEmpty(xml) && !String.IsNullOrEmpty(xml), "Xaml Serialization failed for " + testObject.GetType() + " no xml string available");

            xml = sb.ToString();
            MemoryStream ms = xml.StringToStream();
            object root = XamlReader.Load(ms);
            Assert.IsTrue(root != null, "After reading from MemoryStream no result for Xaml Serialization");

In one of my classes I use the Property Brush. In the above code this Unit Tests fails because a Brush object (not serializable) is the value. When I remove the Setter (as below), the Unit Test passes.

Using the XmlWriter (basically same test as above) it works. In the StringBuffer sb I can see that Property Brush is serialized when the Setter is there and not when removed (most likely another check ignoring the Property because of no setter). Other Properties with [XmlIgnore] are ignored as intended.

    [XmlIgnore]
    public Brush MyBrush
    {
        get { ..... }
        // removed because of problem with Serialization
        // set { ... }
    }
like image 804
Horst Walter Avatar asked Jan 01 '11 20:01

Horst Walter


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Originally Answered: What is the full form of C ? C - Compiler . C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

John's comment is correct. There are (again) other attributes. I found this excellent article here: http://blogs.msdn.com/b/mikehillberg/archive/2006/09/16/xamlwriter.aspx

I even came across the attribute [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] before , but misinterpreted it as a design time attribute.

like image 190
Horst Walter Avatar answered Nov 15 '22 12:11

Horst Walter