1

I have a class generated by the xsd.exe tool. It doesn't matter what class, the same problem occurs always which is that every attribute having a type other than string is being ignored when the object is being serialized.

Deserializing the xml generated by the serialize method generates a valid .NET object having all attributes with type other than string are empty (int, bool, etc.)

I tried modifying the xml, adding the attributes and deserializing again. This seemed to work.

Edit: I have just created a demo schema and generated a C# class using the xsd tool. My schema looks as following:

<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns="http://PwC.C2C.Schemas.Internal.Schema1" xmlns:b="http://schemas.microsoft.com/BizTalk/2003" targetNamespace="http://PwC.C2C.Schemas.Internal.Schema1" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="TestElement1">
          <xs:complexType>
            <xs:attribute name="AttributeBool" type="xs:boolean" />
            <xs:attribute name="AttributeString" type="xs:string" />
            <xs:attribute name="AttributeInt" type="xs:int" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
    </xs:element>
</xs:schema>

And the generated C# class looks like this:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true,    Namespace="http://PwC.C2C.Schemas.Internal.Schema1")]    [System.Xml.Serialization.XmlRootAttribute(Namespace="http://PwC.C2C.Schemas.Internal.Schema1", IsNullable=false)]

public partial class Root {

    private RootTestElement1 testElement1Field;
    /// <remarks/>
  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public RootTestElement1 TestElement1 {
        get {
            return this.testElement1Field;
        }
        set {
            this.testElement1Field = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true,  Namespace="http://PwC.C2C.Schemas.Internal.Schema1")]
public partial class RootTestElement1 {

    private bool attributeBoolField;

    private bool attributeBoolFieldSpecified;

    private string attributeStringField;

    private int attributeIntField;

    private bool attributeIntFieldSpecified;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public bool AttributeBool {
        get {
        return this.attributeBoolField;
        }
        set {
            this.attributeBoolField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool AttributeBoolSpecified {
        get {
            return this.attributeBoolFieldSpecified;
        }
        set {
            this.attributeBoolFieldSpecified = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string AttributeString {
        get {
            return this.attributeStringField;
        }
        set {
            this.attributeStringField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public int AttributeInt {
        get {
        return this.attributeIntField;
    }
        set {
            this.attributeIntField = value;
        }
    }

    /// <remarks/>
     [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool AttributeIntSpecified {
        get {
            return this.attributeIntFieldSpecified;
        }
        set {
            this.attributeIntFieldSpecified = value;
        }
    }
}

Interesting are the extra properties added if the specified type of the attribute in the schema is no string. attributeIntFieldSpecified and attributeBoolFieldSpecified.

These cause the attributes to be ignored when serializing the class and they need to be completely removed or set to true.

dbc
  • 104,963
  • 20
  • 228
  • 340
mh133
  • 135
  • 12
  • 1
    One possibility is that you have some `xxxSpecified` properties. Take a look at [this answer](https://stackoverflow.com/a/37842985/3744182) to [ShouldSerialize*() vs *Specified Conditional Serialization Pattern](https://stackoverflow.com/q/37838640/3744182) which states, regarding the `{PropertyName}Specified` pattern, ... – dbc Nov 13 '18 at 20:12
  • 1
    ... *`xsd.exe` will sometimes generate it for you automatically, however the interaction between a property and its `Specified` property is weird and liable to produce bugs. You can fill up all the properties in your class, then serialize to XML and lose **everything** because you didn't also set set the corresponding `Specified` properties to `true`.* – dbc Nov 13 '18 at 20:12
  • @dbc yes this is a {PropertyName}Sepceified issue. Thanks – mh133 Nov 14 '18 at 09:42

0 Answers0