question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deserialisation terminates silently at element for property marked with XmlIgnoreAttribute

See original GitHub issue

This is the last of my questions about deserialising legacy documents, I promise! 😉

I am finding that ExtendedXmlSerializer.Deserialize() stops processing the document if it encounters an element that corresponds to a property that is marked with XmlIgnoreAttribute. Small example below. You can see that in each case, the document is deserialised correctly, up until the first occurrence of Entity.Show (not including the override, Entity2.Show).

I think I can work around it, but maybe it is a bug? If XmlIgnoreAttribute was added to an existing member, I think it would break deserialisation of existing documents created by ExtendedXmlSerializer.

    using System.Collections.Generic;
    using System.Runtime.Serialization;
    using System.Xml.Serialization;

    using ExtendedXmlSerializer.Configuration;
    using ExtendedXmlSerializer.ExtensionModel.Xml;

    public static class ExtendedXmlSerializerTests
    {
        public static void TerminateOnXmlIgnore()
        {
            var serializer = new ConfigurationContainer()
                .EnableImplicitTyping(typeof(Entity), typeof(Entity2))
                .Create();

            // language=XML
            const string content1 =
                @"<?xml version=""1.0"" encoding=""utf-8""?>
                <Entity>
                    <Name>Foo</Name>
	                <Show>false</Show>
                    <Children>
                        <Entity>
                            <Name>Bar</Name>
                            <Show>false</Show>
                        </Entity>
                        <Entity>
                            <Name>Jim</Name>
                            <Show>true</Show>
                        </Entity>
                    </Children>
                </Entity>";

            const string content2 =
                @"<?xml version=""1.0"" encoding=""utf-8""?>
                <Entity>
                    <Name>Foo</Name>
                    <Children>
                        <Entity>
                            <Name>Bar</Name>
                            <Show>false</Show>
                        </Entity>
                        <Entity>
                            <Name>Jim</Name>
                            <Show>true</Show>
                        </Entity>
                    </Children>
	                <Show>false</Show>
                </Entity>";

            const string content3 =
                @"<?xml version=""1.0"" encoding=""utf-8""?>
                <Entity2>
                    <Name>Foo</Name>
                    <Children>
                        <Entity2>
                            <Name>Bar</Name>
                            <Show>false</Show>
                        </Entity2>
                        <Entity2>
                            <Name>Jim</Name>
                            <Show>true</Show>
                        </Entity2>
                    </Children>
	                <Show>false</Show>
                </Entity2>";

            // result1.Children.Count = 0
            var result1 = serializer.Deserialize<Entity>(content1);

            // result2.Children.Count = 1
            var result2 = serializer.Deserialize<Entity>(content2);

            // result3.Children.Count = 2
            var result3 = serializer.Deserialize<Entity2>(content3);
        }
    }

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

        [XmlIgnore]
        public virtual bool Show
        {
            get => true;
            set { }
        }

        public List<Entity> Children { get; } = new List<Entity>();
    }

    public class Entity2 : Entity
    {
        [DataMember]
        public override bool Show { get; set; }
    }

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
StuYarrowcommented, Nov 7, 2019

Many thanks! I have the EXS project in my solution for debugging at the moment, so its easy to pull in those changes.

0reactions
Mike-E-angelocommented, Dec 3, 2019

This has been released in the latest version which can be found here:

https://www.nuget.org/packages/ExtendedXmlSerializer/

Further details on the release can be found here:

https://github.com/ExtendedXmlSerializer/home/releases/tag/3.0.0

If you run into any problems or have further questions, please feel free to open a new issue and we’ll take a look. Thank you for improving ExtendedXmlSerializer!

<div>ExtendedXmlSerializer 3.0.0</div><div>An extensible Xml Serializer for .NET that builds on the functionality of the classic XmlSerializer with a powerful and robust extension model.</div>
<div> GitHub</div><div>ExtendedXmlSerializer/home</div><div>A highly configurable and eXtensible Xml serializer for .NET. - ExtendedXmlSerializer/home</div>
Read more comments on GitHub >

github_iconTop Results From Across the Web

.net - XmlIgnoreAttribute, only used during serialization, not ...
If you tag a property with XmlIgnore , it is ignored. It is not considered when the XmlSerializer builds its serialisation assembly.
Read more >
XmlAttributes.XmlIgnore Property (System.Xml.Serialization)
Gets or sets a value that specifies whether or not the XmlSerializer serializes a public field or public read/write property.
Read more >
XmlIgnoreAttribute Class (System.Xml.Serialization)
Instructs the Serialize(TextWriter, Object) method of the XmlSerializer not to serialize the public field or public read/write property value.
Read more >
XmlSerializer and 'not expected' Inherited Types
His problem was that Color didn't serialize correctly, and he got around the problem by putting an XmlIgnore attribute on his Color property...
Read more >
XML Articles
The default action, when serializing objects to XML, is for each public property and field to generate an XML element. The XmlAttribute attribute...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found