Search code examples
c#xmlxml-deserialization

Problem with deserialized classes with Visual Studio


This is my first experience with the deserializer. I'm working on an Xml file, which I don't publish because it's too large, using Visual Studio's 2019 Paste Special to create the classes. However, a strange thing happens, all the classes that have this type of tag: <value xsi:type="CD" code="60975-0" codeSystem="2.16.840.1.113883.6.1" codeSystemName="LOINC" displayName="Displayed Name"/> The property type is not deserialized and, therefore, is not visible in the class. I don't understand the reason for this behavior, but anyway I inserted the missing code for the type attribute.

 
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string type
    {
        get
        { return this.typeField; }
        set
        { this.typeField = value; }
    }

But I can't insert the xsi: prefix before type. I looked at these links How to add xsi:type attribute to an XML element https://www.codeproject.com/Questions/835371/Create-XML-Node-with-attribute-xsi-type But, with my little experience, I couldn't figure out how to fix the problem. I tried replacing this:

    [System.Xml.Serialization.XmlAttributeAttribute()]

with this:

 [System.Xml.Serialization.XmlAttribute(AttributeName="xsi:type")]

but receive this error: InvalidOperationException: 'xsi:type' contains an invalid character for a name.


Solution

  • I found the solution, in the classes where it is needed just add this line:

        [System.Xml.Serialization.XmlAttribute(AttributeName = "type",Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
    

    Thank you all