Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6947129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:39:16+00:00 2026-05-27T13:39:16+00:00

I’m using System.Xaml.XamlServices.Save method to serialize an object which has properties with public getters/private

  • 0

I’m using System.Xaml.XamlServices.Save method to serialize an object which has properties with public getters/private setters and by design these properties are ignored. I tried to implement advice of how to override default XAML bindings and get private properties serialized, but it doesn’t work for some reason – those properties are still ignored. Could anyone point out what’s wrong:

public class CustomXamlSchemaContext : XamlSchemaContext
{
    protected override XamlType GetXamlType(string xamlNamespace, string name, params XamlType[] typeArguments)
    {
        var type = base.GetXamlType(xamlNamespace, name, typeArguments);
        return new CustomXamlType(type.UnderlyingType, type.SchemaContext, type.Invoker);
    }
}

public class CustomXamlType : XamlType
{
    public CustomXamlType(Type underlyingType, XamlSchemaContext schemaContext, XamlTypeInvoker invoker) : base(underlyingType, schemaContext, invoker)
    {
    }

    protected override bool LookupIsConstructible()
    {
        return true;
    }

    protected override XamlMember LookupMember(string name, bool skipReadOnlyCheck)
    {
        var member = base.LookupMember(name, skipReadOnlyCheck);
        return new CustomXamlMember(member.Name, member.DeclaringType, member.IsAttachable);
    }
}

public class CustomXamlMember : XamlMember
{
    public CustomXamlMember(string name, XamlType declaringType, bool isAttachable) : base(name, declaringType, isAttachable)
    {
    }

    protected override bool LookupIsReadOnly()
    {
        return false;
    }
}


    public static string Save(object instance)
    {
        var stringWriter1 = new StringWriter(CultureInfo.CurrentCulture);
        var stringWriter2 = stringWriter1;
        var settings = new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true };
        using (var writer = XmlWriter.Create(stringWriter2, settings))
        {
            Save(writer, instance);
        }
        return stringWriter1.ToString();
    }

    public static void Save(XmlWriter writer, object instance)
    {
        if (writer == null)
            throw new ArgumentNullException("writer");
        using (var xamlXmlWriter = new XamlXmlWriter(writer, new CustomXamlSchemaContext()))
        {
            XamlServices.Save(xamlXmlWriter, instance);
        }
    }

Having above infrastructure code and a class

public class Class1
{
    public string Property1 { get; private set; }
    public string Property2 { get; set; }
    public DateTime AddedProperty { get; set; }
}

and serializing an instance of this class with

var obj = new Class1 { Property1 = "value1", Property2 = "value2" };
var objString = Save(obj);

I get the result

<Class1 AddedProperty="0001-01-01" Property2="value2" xmlns="clr-namespace:TestNamespace;assembly=Tests" />

where there is no entry for Property1.

What’s even more interesting, that none of the overloads are called during serialization.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:39:17+00:00Added an answer on May 27, 2026 at 1:39 pm

    It turned out couple tweaks to my initial code solves the problem. Here’s final solution:

    private class CustomXamlSchemaContext : XamlSchemaContext
    {
        public override XamlType GetXamlType(Type type)
        {
            var xamlType = base.GetXamlType(type);
            return new CustomXamlType(xamlType.UnderlyingType, xamlType.SchemaContext, xamlType.Invoker);
        }
    }
    
    private class CustomXamlType : XamlType
    {
        public CustomXamlType(Type underlyingType, XamlSchemaContext schemaContext, XamlTypeInvoker invoker)
            : base(underlyingType, schemaContext, invoker)
        {
        }
    
        protected override bool LookupIsConstructible()
        {
            return true;
        }
    
        protected override XamlMember LookupMember(string name, bool skipReadOnlyCheck)
        {
            var member = base.LookupMember(name, skipReadOnlyCheck);
            return member == null ? null : new CustomXamlMember((PropertyInfo)member.UnderlyingMember, SchemaContext, member.Invoker);
        }
    
        protected override IEnumerable<XamlMember> LookupAllMembers()
        {
            foreach (var member in base.LookupAllMembers())
            {
                var value = new CustomXamlMember((PropertyInfo)member.UnderlyingMember, SchemaContext, member.Invoker);
                yield return value;
            }
        }
    
        protected override bool LookupIsPublic()
        {
            return true;
        }
    }
    
    private class CustomXamlMember : XamlMember
    {
        public CustomXamlMember(PropertyInfo propertyInfo, XamlSchemaContext schemaContext, XamlMemberInvoker invoker)
            : base(propertyInfo, schemaContext, invoker)
        {
        }
    
        protected override bool LookupIsReadOnly()
        {
            return false;
        }
    
        protected override bool LookupIsWritePublic()
        {
            return true;
        }
    }
    

    This customization allows to serialize/deserialize properties with public getter and public/internal/protected/private setters. It ignores all the other properties. It also serializes instances of internal classes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to count how many characters a certain string has in PHP, but
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.