Deserialize collection of elements
See original GitHub issueHi,
I want to deserialze a collection. My XML structure
<MediaContainer size='1'>
<MediaItem Name='Name1' Id='1' />
<MediaItem Name='Name2' Id='2' />
</MediaContainer>
My class structure
public class MediaContainer
{
[XmlAttribute("size")]
public int Size { get; set; }
[XmlElement("MediaItem")]
public List<MediaItem> Items { get; set; }
}
public class MediaItem
{
[XmlAttribute]
public string Name { get; set; }
[XmlAttribute]
public string Id { get; set; }
}
I’ve tried different configuration, but it’s not working.
var serializer = new ConfigurationContainer() .EnableImplicitTyping(typeof(MediaContainer), typeof(List<MediaItem>), typeof(MediaItem)) .Type<List<MediaItem>>() .Create();
I got exception:
System.NullReferenceException: Object reference not set to an instance of an object.
at ExtendedXmlSerializer.ReflectionModel.TypeCoercer.Get(TypeInfo parameter) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\ReflectionModel\TypeCoercer.cs:line 35
at System.Linq.Enumerable.SelectArrayIterator`2.ToArray()
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at System.Collections.Immutable.ImmutableArray.CreateRange[T](IEnumerable`1 items)
at System.Collections.Immutable.ImmutableArray.ToImmutableArray[TSource](IEnumerable`1 items)
at ExtendedXmlSerializer.ReflectionModel.SelectCoercer`2.Get(ImmutableArray`1 parameter) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\ReflectionModel\SelectCoercer.cs:line 37
at ExtendedXmlSerializer.Core.Sources.CoercedResult`3.Get(TParameter parameter) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\Core\Sources\CoercedResult.cs:line 37
at ExtendedXmlSerializer.Core.Sources.CoercedResult`3.Get(TParameter parameter) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\Core\Sources\CoercedResult.cs:line 37
at ExtendedXmlSerializer.Core.Sources.CoercedResult`3.Get(TParameter parameter) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\Core\Sources\CoercedResult.cs:line 37
at ExtendedXmlSerializer.Core.Sources.DecoratedSource`2.Get(TParameter parameter) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\Core\Sources\DecoratedSource.cs:line 32
at ExtendedXmlSerializer.ContentModel.Collections.Lists.CreateAdapter(Object parameter) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\ContentModel\Collections\Lists.cs:line 47
at System.Runtime.CompilerServices.ConditionalWeakTable`2.GetValueLocked(TKey key, CreateValueCallback createValueCallback)
at System.Runtime.CompilerServices.ConditionalWeakTable`2.GetValue(TKey key, CreateValueCallback createValueCallback)
at ExtendedXmlSerializer.ExtensionModel.Xml.XmlListContentsActivator.Create(IFormatReader reader, Object instance, XmlContent content) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\ExtensionModel\Xml\XmlListContentsActivator.cs:line 41
at ExtendedXmlSerializer.ExtensionModel.Xml.XmlInnerContentActivator.Get(IFormatReader parameter) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\ExtensionModel\Xml\XmlInnerContentActivator.cs:line 50
at ExtendedXmlSerializer.ContentModel.Content.InnerContentReader.Get(IFormatReader parameter) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\ContentModel\Content\InnerContentReader.cs:line 43
at ExtendedXmlSerializer.ContentModel.Members.Extensions.GetIfAssigned[T](IReader`1 this, IFormatReader reader) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\ContentModel\Members\Extensions.cs:line 35
at ExtendedXmlSerializer.ContentModel.Members.Extensions.GetIfAssigned[T](IReader`1 this, IFormatReader reader) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\ContentModel\Members\Extensions.cs:line 35
at ExtendedXmlSerializer.ContentModel.Members.MemberHandler.Handle(IInnerContent contents, IMemberSerializer member) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\ContentModel\Members\MemberHandler.cs:line 37
at ExtendedXmlSerializer.ContentModel.Members.MemberInnerContentHandler.IsSatisfiedBy(IInnerContent parameter) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\ContentModel\Members\MemberInnerContentHandler.cs:line 50
at ExtendedXmlSerializer.ContentModel.Members.MemberInnerContentHandler.Execute(IInnerContent parameter) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\ContentModel\Members\MemberInnerContentHandler.cs:line 59
at ExtendedXmlSerializer.ContentModel.Content.InnerContentReader.Get(IFormatReader parameter) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\ContentModel\Content\InnerContentReader.cs:line 46
at ExtendedXmlSerializer.ContentModel.Members.Extensions.GetIfAssigned[T](IReader`1 this, IFormatReader reader) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\ContentModel\Members\Extensions.cs:line 35
at ExtendedXmlSerializer.Read`1.Get(T parameter) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\Read.cs:line 49
at ExtendedXmlSerializer.ExtensionModel.Xml.Extensions.Deserialize[T](IExtendedXmlSerializer this, IXmlReaderFactory factory, Stream stream) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\ExtensionModel\Xml\Extensions.cs:line 291
How I can configure container to deserialize my xml?
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
How to deserialize a collection with ...
It threw this Exception: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Root]' ...
Read more >Supported collection types in System.Text.Json
This article gives an overview of which collections are supported for serialization and deserialization. System.Text.Json.
Read more >Deserialize a Collection
This sample deserializes JSON into a collection. ... DeserializeObject<List<string>>(json); Console.WriteLine(string.Join(", ", videogames.
Read more >JSON.deserialize with arrays with one to many elements
Hi @Keith, but my question is regarding the JSON.deserialize operation and the APEX classes that we have. If the number of expected elements...
Read more >Deserialization of collection does not support single ...
An exception is thrown when trying to deserialize a list of elements that contains a single element only and does not have an...
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
Hey @martin211 thanks for writing in here.
You were close. 😃 I have created an example of a passing test here that shows you how to configure your container:
https://github.com/wojtpl2/ExtendedXmlSerializer/blob/ea48cfcbb262734af7a9159bb1a8ad45a0f75a2f/test/ExtendedXmlSerializer.Tests/ReportedIssues/Issue243Tests.cs#L15-L40
Please note that:
Issue243Tests-
as the classes used have been put into the containing test as nested classes.XmlElementAttribute
to signify child content is not supported in v2 currently. It is a known issue and captured in #240. In the meantime, you will have to specify aMediaItem
as shown in the example.Please let me know if you have any further questions and I will do my best to assist. 👍
@Mike-EEE Hmm… XSLT… you just gave me a good idea!!!