0

I have the following XML, but I have some problems deserializing it.

Part of XML:

<message:AIXMBasicMessage gml:id="uniqueId">
    <message:hasMember xlink:type="simple">
        <aixm:VerticalStructure gml:id="uuid.xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">
          .....
        </aixm:VerticalStructure>
    </message:hasMember>
</message:AIXMBasicMessage>

I have these classes in VB.net

<XmlRoot(ElementName:="hasMember")>
Public Class HasMember
    <XmlElement(ElementName:="VerticalStructure")>
    Public Property VerticalStructure As VerticalStructure
    <XmlAttribute(AttributeName:="type")>
    Public Property Type As String
    <XmlText>
    Public Property Text As String
End Class

<XmlRoot(ElementName:="AIXMBasicMessage")>
Public Class AIXMBasicMessage
    <XmlElement(ElementName:="hasMember")>
    Public Property HasMember As List(Of HasMember)
End Class

<XmlRoot(ElementName:="Root")>
Public Class Root
    <XmlElement(ElementName:="AIXMBasicMessage")>
    Public Property AIXMBasicMessage As AIXMBasicMessage
    <XmlAttribute(AttributeName:="message")>
    Public Property Message As String
    <XmlAttribute(AttributeName:="xlink")>
    Public Property Xlink As String
    <XmlAttribute(AttributeName:="aixm")>
    Public Property Aixm As String
    <XmlAttribute(AttributeName:="gml")>
    Public Property Gml As String
    <XmlText>
    Public Property Text As String
End Class

the problem in message: / aixm: / xlink: / gml: because, if I delete it from the XML file it works fine. But how do I change the code, so I don't need to change the XML file to get it to be read?

jpi76
  • 13
  • 3
  • 1
    This post will point you in the right direction. You need to give it the namespaces when you deserialize: https://stackoverflow.com/questions/27365029/deserializing-xml-with-namespace-and-multiple-nested-elements – Fawlty Jun 20 '22 at 13:37
  • @Fawlty Thanks. I'm new to the XML deserialization and I have tried different approaches, but I cant get it to work. Also the documentation hasn't been usefull. Could I get a more precise answer? Thanks :) – jpi76 Jun 21 '22 at 08:20
  • Please take the time to read: https://learn.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlserializer.-ctor?view=net-6.0#system-xml-serialization-xmlserializer-ctor(system-type-system-xml-serialization-xmlrootattribute) It's important that your classes you are deserializing to match the namespaces in the XML and that you initiate the the deserializer with the correct root namespace. – Fawlty Jun 21 '22 at 08:35
  • Please provide enough code so others can better understand or reproduce the problem. – Community Jun 21 '22 at 10:00

1 Answers1

0

I got it working.

Here is the code for the classes:

<XmlRoot(ElementName:="VerticalStructure", [Namespace]:="http://www.aixm.aero/schema/5.1")>
Public Class VerticalStructure
    <XmlElement(ElementName:="identifier", [Namespace]:="http://www.opengis.net/gml/3.2")>
    Public Property Identifier As Identifier
    <XmlElement(ElementName:="timeSlice", [Namespace]:="http://www.aixm.aero/schema/5.1")>
    Public Property TimeSlice As TimeSlice
    <XmlAttribute(AttributeName:="id", [Namespace]:="http://www.opengis.net/gml/3.2")>
    Public Property Id As String
End Class

<XmlRoot(ElementName:="hasMember", [Namespace]:="http://www.aixm.aero/schema/5.1/message")>
Public Class HasMember
    <XmlElement(ElementName:="VerticalStructure", [Namespace]:="http://www.aixm.aero/schema/5.1")>
    Public Property VerticalStructure As VerticalStructure
    <XmlAttribute(AttributeName:="type", [Namespace]:="http://www.w3.org/1999/xlink")>
    Public Property Type As String
End Class

<XmlRoot(ElementName:="AIXMBasicMessage", [Namespace]:="http://www.aixm.aero/schema/5.1/message")>
Public Class AIXMBasicMessage
    <XmlElement(ElementName:="hasMember", [Namespace]:="http://www.aixm.aero/schema/5.1/message")>
    Public Property HasMember As List(Of HasMember)
    <XmlAttribute(AttributeName:="id", [Namespace]:="http://www.opengis.net/gml/3.2")>
    Public Property Id As String
    <XmlAttribute(AttributeName:="schemaLocation", [Namespace]:="http://www.w3.org/2001/XMLSchema-instance")>
    Public Property SchemaLocation As String
    <XmlAttribute(AttributeName:="gss", [Namespace]:="http://www.w3.org/2000/xmlns/")>
    Public Property Gss As String
    <XmlAttribute(AttributeName:="gts", [Namespace]:="http://www.w3.org/2000/xmlns/")>
    Public Property Gts As String
    <XmlAttribute(AttributeName:="gsr", [Namespace]:="http://www.w3.org/2000/xmlns/")>
    Public Property Gsr As String
    <XmlAttribute(AttributeName:="gml", [Namespace]:="http://www.w3.org/2000/xmlns/")>
    Public Property Gml As String
    <XmlAttribute(AttributeName:="message", [Namespace]:="http://www.w3.org/2000/xmlns/")>
    Public Property Message As String
    <XmlAttribute(AttributeName:="adr", [Namespace]:="http://www.w3.org/2000/xmlns/")>
    Public Property Adr As String
    <XmlAttribute(AttributeName:="xsi", [Namespace]:="http://www.w3.org/2000/xmlns/")>
    Public Property Xsi As String
    <XmlAttribute(AttributeName:="adfe", [Namespace]:="http://www.w3.org/2000/xmlns/")>
    Public Property Adfe As String
    <XmlAttribute(AttributeName:="aixm", [Namespace]:="http://www.w3.org/2000/xmlns/")>
    Public Property Aixm As String
    <XmlAttribute(AttributeName:="gco", [Namespace]:="http://www.w3.org/2000/xmlns/")>
    Public Property Gco As String
    <XmlAttribute(AttributeName:="ids", [Namespace]:="http://www.w3.org/2000/xmlns/")>
    Public Property Ids As String
    <XmlAttribute(AttributeName:="event", [Namespace]:="http://www.w3.org/2000/xmlns/")>
    Public Property [Event] As String
    <XmlAttribute(AttributeName:="xlink", [Namespace]:="http://www.w3.org/2000/xmlns/")>
    Public Property Xlink As String
    <XmlAttribute(AttributeName:="gmd", [Namespace]:="http://www.w3.org/2000/xmlns/")>
    Public Property Gmd As String
End Class
jpi76
  • 13
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 21 '22 at 14:52