6
Vote

PascalCase and XmlArrayItemAttribute

description

Thanks for the PascalCase feature in 3.5 beta. It's really useful.
 
I found a problem when using PascalCase with XmlArray. Given the following XML:
 
message>
<actionseg> 
      <actioncode>CC</actioncode> 
</actionseg> 
<alternatelistseg>
       <listrecord>
             <prodcode>ARS</prodcode>
       </listrecord>
 </alternatelistseg>
</message>
 
And the following generated code (slighly changed):
 
public partial class Message
{
 
    private MessageActionseg actionsegField;
 
    private List<MessageListrecord> alternatelistsegField;
 
    [System.Xml.Serialization.XmlElementAttribute("actionseg")]
    public MessageActionseg Actionseg
    {
        get
        {
            if ((this.actionsegField == null))
            {
                this.actionsegField = new MessageActionseg();
            }
            return this.actionsegField;
        }
        set
        {
            this.actionsegField = value;
        }
    }
 
    [System.Xml.Serialization.XmlArrayItemAttribute("listrecord", IsNullable = false)]
    [System.Xml.Serialization.XmlElementAttribute("alternatelistseg")]
    public List<MessageListrecord> Alternatelistseg
    {
        get
        {
            if ((this.alternatelistsegField == null))
            {
                this.alternatelistsegField = new List<MessageListrecord>();
            }
            return this.alternatelistsegField;
        }
        set
        {
            this.alternatelistsegField = value;
        }
    }
}
 
Xsd2Code will try to mix up XmlElementAttribute and XmlArrayItemAttribute which will fall into the following exception:
 
  • XmlElement, XmlText, and XmlAnyElement cannot be used in conjunction with XmlAttribute, XmlAnyAttribute, XmlArray, or XmlArrayItem.
     
    The right attributes should be:
     
    [System.Xml.Serialization.XmlArrayItemAttribute("listrecord", IsNullable = false)]
    [System.Xml.Serialization.XmlArray("alternatelistseg")]
     
    Thanks for your help. Thanks for this tool.
     
    Leo

comments

scotthill1980 wrote Sep 28, 2012 at 2:45 PM

Thanks for the new PascalCase feature - I've been after this for some time ;o)

I'm seeing the same issue with the attributes mentioned above :o(

In addition, when using PascalCase I think you need to add the [XmlRoot()] attribute to the class and define the non pascal case element name. Without this, the classes do not de serialise correctly when used directly.

e.g. [XmlRoot("camelCaseName")]

Otherwise a great tool. Thanks for your hard work.

Scott

jayjoe wrote Jan 25, 2013 at 1:51 PM

Hello, i would be very thankfull, if someone could correct the error above.
Therefore i'll try to explain it a bit more.

At the Moment it may be produced code like this:
[System.Xml.Serialization.XmlArrayItemAttribute("bar", IsNullable = false)]
        [System.Xml.Serialization.XmlElementAttribute("foo")]
        public List<SendemitschnittType> Foo
        {
            get;
            set;
        }
When de-/serializing an xml it will throw errors because XmlElementAttribute and XmlArrayItemAttribute may not be used side-by-side, correct would be to have XmlArrayAttribute instead of XmlElementAttribute.

this failure will only happen when PascalCase PropertyName Generation is activated

xsd code to produce error
      <xs:element name="foo">
        <xs:complexType>
          <xs:sequence>
            <xs:element maxOccurs="unbounded" name="bar" type="shortString" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>