logo
down
shadow

IEnumerable(of String) ignored by XmlSerializer


IEnumerable(of String) ignored by XmlSerializer

By : LHuntington
Date : November 26 2020, 06:28 AM
To fix this issue For one of my object, I need to do some backward/forward compatibility tricks so that everyone is happy while different versions coexist and share the same config file. , You have a few issues here:
code :
[System.InvalidOperationException: To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy. EnumerableBridge`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[CategoryInfo, 58be8a1c-a824-4133-9ef8-b2552cccedab, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]] does not implement Add(System.Object).]
' <Serializable> Removed since _converterFrom and _add are not really serializable
Public Class EnumerableBridge(Of TSource, TTarget)
    Implements IEnumerable(Of TSource)

    Private _refCollection As IList(Of TTarget)
    Private _add As Action(Of IList(Of TTarget), TSource)
    Private _converterFrom As Func(Of TTarget, TSource)

    Sub New()
        Throw New NotImplementedException()
    End Sub

    Public Sub New(refCollection As IList(Of TTarget), add As Action(Of IList(Of TTarget), TSource), converterFrom As Func(Of TTarget, TSource))
        _refCollection = refCollection
        _add = add
        _converterFrom = converterFrom
    End Sub

    Public Sub Add(item As TSource) 'Implements ICollection(Of TSource).Add
        _add(_refCollection, item)
    End Sub

    Public Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
        Return GetEnumerator()
    End Function

    Public Function GetEnumerator() As IEnumerator(Of TSource) Implements IEnumerable(Of TSource).GetEnumerator
        Return _refCollection.Select(_converterFrom).GetEnumerator()
    End Function
End Class
Public Class OptionsSerializable
    Public Sub New()
    End Sub

    <XmlAnyElement>
    Public Property Unsupported As List(Of XmlElement)

    <System.Xml.Serialization.XmlElement(ElementName:="ListeCategoriesExt")>
    Public ReadOnly Property Categories As List(Of CategoryInfo) = New List(Of CategoryInfo)

    Shared Sub AddCategory(Categories as IList(Of CategoryInfo), Name as String)
        If Not Categories.Any(Function(c) c.SearchTerm = Name) Then
            Categories.Add(New CategoryInfo(Name))
        End If
    End Sub

    Public ReadOnly Property ListeCategories As EnumerableBridge(Of String, CategoryInfo) = New EnumerableBridge(Of String, CategoryInfo)(_categories, AddressOf AddCategory, Function(c) c.SearchTerm)

    ' Remainder unchanged


Share : facebook icon twitter icon
How can I use IEnumerable interface and at the same time, have XMLSerializer not use GetEnumerator()?

How can I use IEnumerable interface and at the same time, have XMLSerializer not use GetEnumerator()?


By : Yuks
Date : March 29 2020, 07:55 AM
seems to work fine I managed to fix this problem without having to change my design. None of my code was relying on the IEnumerable interface, just the implementation of IEnumerable GetEnumerator() (apparently foreach doesn't check to see if IEnumerable is implemented). Just commenting out the interface in the class declaration did the trick.
XmlSerializer and IEnumerable: Serialization possible w/o parameterless constructor: Bug?

XmlSerializer and IEnumerable: Serialization possible w/o parameterless constructor: Bug?


By : user2641024
Date : March 29 2020, 07:55 AM
To fix this issue The reason for the behavior is that this is the way it has always worked.
From XmlSerializer class:
XmlSerializer - IEnumerable or IList as LIst

XmlSerializer - IEnumerable or IList as LIst


By : merbng
Date : March 29 2020, 07:55 AM
it fixes the issue Since you can't change the class, create a new class, inheriting from the class you want to serialize and from IXmlSerializable. In addition, you can override the base Colors array with the new keyword.
Try this one:
code :
public class Something
{
    public int Id { get; set; }

    public string Text { get; set; }

    public IEnumerable<string> Colors { get; set; }
}

public class MySerializableSomething : Something, IXmlSerializable
{
    public new List<string> Colors { get; set; }

    public MySerializableSomething()
    {
        Colors = new List<string>();
    }

    public XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(XmlReader reader)
    {
        while (reader.Read())
        {
            switch (reader.LocalName)
            {
                case "Id": Id = reader.ReadElementContentAsInt(); break;
                case "Text": Text = reader.ReadElementContentAsString(); break;
                case "Color": Colors.Add(reader.ReadElementContentAsString()); break;
            }
        }
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteElementString("Id", Id.ToString());
        writer.WriteElementString("Text", Text);

        writer.WriteStartElement("Colors");

        foreach (var color in Colors)
        {
            writer.WriteElementString("Color", color);
        }

        writer.WriteEndElement();
    }
}
XmlSerializer not deserializing XML to IEnumerable

XmlSerializer not deserializing XML to IEnumerable


By : user3541820
Date : March 29 2020, 07:55 AM
To fix this issue When your XML-serialized type has a property of type List, XmlSerializer will get the value of the property, then call .Add(...) to add the deserialized elements (i.e. it does not set the property).
Since the getter for the GlossarySurrogate property returns a new list every time you get it, the changes the XML serializer makes to the result are lost.
XmlSerializer won't serialize IEnumerable

XmlSerializer won't serialize IEnumerable


By : Domi200
Date : March 29 2020, 07:55 AM
Any of those help The way you serialize an IEnumerable property is with a surrogate property
Related Posts Related Posts :
  • Estimating Amount Of time For Tasks Running in Parallel
  • Opening, Changing, and Saving Excel Sheets with DGV
  • Remapping of Database from one server to other TFS-2018
  • SQL Server: Dynamically create Column Names from JSON Auto Data
  • Is it possible to Install .net 4.7 version on my machine when .net 4.8 is already installed?
  • Getting server absolute path in .NET core 2.1
  • Regex to find last word (including symbols) on line
  • Adding element to a couchbase database with f# code
  • How to create "NuGet Package Management Project" for .NET Standard?
  • Blazor BaseURI http vs https issue
  • Does X590Certificate.Build use OCSP if ChainPolicy RevocationMode Online is used?
  • Error "NU3034 This package is signed but not by a trusted signer"
  • Blazor .net version compatibility issues
  • How to test async Task method
  • this project doesn't know how to run the profile docker
  • Azure Cloud Storage SDK UploadFromStreamAsync storing 0 bytes
  • What is the Relaese date for Microsoft Orleans which can support service fabric?
  • Make a string persistent in a .NET application
  • How do I configure Cosmos DB .NET 3.0 SDK to serialize with camel case?
  • Service segmentation fault in AWS EC2
  • How to avoid the compilation error with inconsistent accessibility of class and methods?
  • "Does not implement IControllerFactory.CreateController" in Visual Studio 2010
  • Tabs for a ASP.NET website
  • AccountManagement.UserPrincipal.Current takes 5 seconds
  • Will linq to sql work in an asp.net 2.0 web application?
  • .Net Regular Expression to get parenthetical text at end of <p> tags
  • The Project Location is Not Trusted error in Visual Studio
  • how to tell if a photo was taken in landscape or portrait? JPEG .NET metadata orientation
  • Winform and ValidateChildren()
  • Why need to mention "Supported by the .NET Compact Framework" for classes members?
  • Pass MSI parameters to .NET installer class
  • How to print stack trace of StackOverflowException
  • OnPropertyChange as an Alternative to Triggers in Entity Framework
  • "User Friendly" .net compatible Regex/Text matching tools?
  • Non-Ascii characters not supported by .net?
  • Linq SqlMethods.Like fails
  • To check whether the SQL server DB is working properly or not
  • What is the Difference between GC.GetTotalMemory(false) and GC.GetTotalMemory(true)
  • Which version of .NET is available for Xbox 360?
  • Validation (with custom ErrorTemplate) for a DataTemplate
  • is the threadpool shared - .net
  • WPF RichTextBox - Formatting of typed text
  • "ResizeEnd" equivalent for usercontrols
  • How work with common utils project
  • Using image resource in XAML markup?
  • How to use XML namespace prefixes without xmlns="..." everywhere? (.NET)
  • How to write an altered COM Structured Storage file to Disk?
  • Implementing auditing using Autofac if it support method interception?
  • How can a static class be resolved by the Unity Framework?
  • How to flush DNS cache in Windows Mobile programmatically?
  • how to set autoplay=true in google audio player?
  • Edit dialog, with bindings and OK/Cancel in WPF
  • What does MailMessage.IsBodyHtml do?
  • C# Class Library wont register for COM
  • Robust DateTime parser library for .NET
  • MySql BulkCopy/Insert from DataReader
  • How do I allow all users access to one route within a website with integrated auth?
  • Reading from a oracle temp table in a separate procedure than the one it was populted in
  • Recommendations for IPC between parent and child processes in .NET?
  • F# 1.9.9.9 and lapack
  • shadow
    Privacy Policy - Terms - Contact Us © festivalmusicasacra.org