1

At this moment I'm struggling to deserialize an XML response from BRO ( part of the Dutch government). They have a public API that can be used to get groundwater levels and details. This is the response( url ).

If I change this part xsi:type="ns11:GLD_O_DPType" with string.replace the XML can be parsed.

From: <ns11:GLD_O xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns11:GLD_O_DPType" gml:id="BRO_0002">

To: <ns11:GLD_O xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" gml:id="BRO_0002">

The XML container class is created true my IDE Visual Studio 2022 via Edit > paste Special > Paste XML As Classes. And this is the generated class

The following code is used to deserialize the XML:

public static T ParseXML<T>(this string @this) where T : class
        {
            if(@this == null) { return null; }
            var reader = XmlReader.Create(@this.Trim().ToStream(), new XmlReaderSettings() { ConformanceLevel = ConformanceLevel.Document });
            return new XmlSerializer(typeof(T)).Deserialize(reader) as T;
        }

// In my mainclass
var parsedResult = ParseHelpers.ParseXML<dispatchDataResponse>(result);

In my mainclass, result is a type of string from the response body of the request (first link)

In short, how can I deserialize the object with the attribute xmlns:xsi

ido435
  • 27
  • 2
  • 5
  • 1
    The type attribute means you have an inherited class. You need to use XmlInclude : https://learn.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlincludeattribute?force_isolation=true&view=net-6.0. You also have xsi so the namespace need to be set. – jdweng May 16 '22 at 09:58
  • *Paste XML As Classes* will not correctly interpret `xsi:type` attributes, which are used to indicate polymorphic subtype. However, if you have an **XSD schema** for your XML file, you can generate correct classes using xsd.exe. Since the XML is from an official government source I'd be willing to bet there is a corresponding XSD, so you should get it and use it for your code generation. For details of why an XSD is preferable see [xsi:type attribute messing up C# XML deserialization](https://stackoverflow.com/a/36365689/3744182). – dbc May 21 '22 at 17:00
  • But beyond pointing you to [xsi:type attribute messing up C# XML deserialization](https://stackoverflow.com/a/36365689/3744182) we probably need to see a [mcve] to help you. See: [ask]. – dbc May 21 '22 at 18:43

1 Answers1

0

In order for the XML Serializer to recognize the type, you need to change the generated code in line 111 from:

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
// The following line needs to be changed
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.broservices.nl/xsd/dsgld/1.0")]
public partial class dispatchDataResponseDispatchDocumentGLD_O
{

    private string broIdField;

to

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
// This asserts that the type name is recognized
[System.Xml.Serialization.XmlTypeAttribute(TypeName = "GLD_O_DPType", Namespace = "http://www.broservices.nl/xsd/dsgld/1.0")]
public partial class dispatchDataResponseDispatchDocumentGLD_O
{

    private string broIdField;
Markus
  • 20,838
  • 4
  • 31
  • 55