questions/42040208/using-xmlattribuetoverrides-to-ignore-elements-not-working

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.Threading;
using System.ComponentModel;
using System.Xml.Linq;
using System.Reflection;
using System.Data;
using System.Xml.Schema;
using System.Net;
using System.Xml.XPath;
using System.Collections;

    // https://stackoverflow.com/questions/42040208/using-xmlattribuetoverrides-to-ignore-elements-not-working

    public class Parent
    {
        static class Nested
        {
            internal static readonly XmlSerializer xs;

            static Nested()
            {
                XmlAttributes ignore = new XmlAttributes()
                {
                    XmlIgnore = true
                };

                XmlAttributeOverrides overrides = new XmlAttributeOverrides();
                //overrides.Add(typeof(SomeClass), "MyProperty", ignore); // Does not work
                overrides.Add(typeof(Parent), "MyProperty", ignore);
                overrides.Add(typeof(Child), "MyProperty", ignore);

                xs = new XmlSerializer(typeof(Parent), overrides);                
            }
        }

        public static XmlSerializer Serializer
        {
            get
            {
                // Using Lazy singleton pattern
                // http://csharpindepth.com/Articles/General/Singleton.aspx#nested-cctor
                return Nested.xs;
            }
        }

        public SomeClass MyProperty { get; set; }
        public List<Child> Children { get; set; }
    }

    public class Child
    {
        public SomeClass MyProperty { get; set; }
    }

    public class SomeClass
    {
        public string Name { get; set; }
    }

    class TestClass
    {
        internal static void Test()
        {
            var xs = Parent.Serializer;

            var test = new Parent { MyProperty = new SomeClass { Name = "I should not appear" }, Children = new List<Child> { new Child { MyProperty = new SomeClass { Name = "I should not appear" } } } };

            var xml = test.GetXml(xs);

            Console.WriteLine(xml);
			
			Assert.IsTrue(!xml.Contains("MyProperty"));
			Console.WriteLine("\"MyProperty\" is not present in XML.");
        }
    }					

	public class Program
	{
		public static void Main()
		{
			TestClass.Test();
		}
	}

    public static class XmlSerializationHelper
    {
        public static T LoadFromXml<T>(this string xmlString)
        {
            using (StringReader reader = new StringReader(xmlString))
            {
                return (T)new XmlSerializer(typeof(T)).Deserialize(reader);
            }
        }

        public static string GetXml<T>(this T obj, XmlSerializer serializer = null, bool omitStandardNamespaces = false)
        {
            XmlSerializerNamespaces ns = null;
            if (omitStandardNamespaces)
            {
                ns = new XmlSerializerNamespaces();
                ns.Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines.
            }			
            using (var textWriter = new StringWriter())
            {
                var settings = new XmlWriterSettings() { Indent = true }; // For cosmetic purposes.
                using (var xmlWriter = XmlWriter.Create(textWriter, settings))
                    (serializer ?? new XmlSerializer(obj.GetType())).Serialize(xmlWriter, obj, ns);
                return textWriter.ToString();
            }
        }
    }

	public class AssertionFailedException : System.Exception
    {
        public AssertionFailedException() : base() { }

        public AssertionFailedException(string s) : base(s) { }
    }

    public static class Assert
    {
        public static void IsTrue(bool value)
        {
            if (value == false)
                throw new AssertionFailedException("failed");
        }
    }
;