Reputation: 39
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
Upvotes: 1
Views: 758
Reputation: 22501
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;
Upvotes: 0