Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

XmlSerializer error deserializing to XElement in Blazor WebAssembly

$
0
0

I have a Blazor web project that I encountered a problem with when I published to a test server. Everything had been working when running in VS2026. I open an XML document via a stream and deserialize it to a Property object. One of the properties is a map object that contains KML data. I just want this to be an XElement I can traverse instead of trying to build out a KML data type. The (truncated) XML looks like this:

<Property><!-- Other elements --><PropertyMap><KML><!-- KML Elements --></KML></PropertyMap></Property>

And the class I am deserializing to is:

[XmlRoot]public class Property{    // Other properties omitted.    [XmlElement("PropertyMap")]    public PropertyMap Map { get; set; }}public class PropertyMap{    public XElement? KML { get; set;}}

And finally, I am deserializing it like this:

XmlSerializer serializer = new XmlSerializer(typeof(Property));Property? property = (Property?)serializer.Deserialize(fileStream);

When I publish this to IIS and run it using WebAssembly render mode, I get this error:

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: XmlTypeReflectionError, Property
System.InvalidOperationException: XmlTypeReflectionError, Property
---> System.InvalidOperationException: XmlPropertyReflectionError, Map
---> System.InvalidOperationException: XmlTypeReflectionError, PropertyMap
---> System.InvalidOperationException: XmlSerializerUnsupportedMember, PropertyMap.KML, System.Xml.Linq.XElement
---> System.InvalidOperationException: XmlConstructorInaccessible, System.Xml.Linq.XElement
Exception_EndOfInnerExceptionStack

If I switch the app to use Server render mode, the deserialization works fine. When I run it from Visual Studio using either WebAssembly or Server, it works. It only errors when publishing using WebAssembly.

I've tried publishing using both -c Release and -c Debug and it makes no difference, same error. I would assume this is because XElement has no public parameterless constructor, but in that case, why does deserialization work in the other scenarios?

In summary:

Works in VS2026 with either Blazor Server or WebAssembly.

Doesn't work when published (either IIS or running straight Kestrel) in Blazor WebAssembly. (Release or Debug)


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>