Deserialisation terminates silently at element for property marked with XmlIgnoreAttribute
See original GitHub issueThis 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:
- Created 4 years ago
- Reactions:1
- Comments:7 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Many thanks! I have the EXS project in my solution for debugging at the moment, so its easy to pull in those changes.
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!