C# classes generated from XSD contains properties marked with different qualification attributes

Viewed 1053

I cannot understand why C# code generators (xsd, xsd2code) produce output class Profile with two properties of the same type marked with different attributes. One of them is marked as Unqualified and one is not.

My XSD looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.acme.com" xmlns="http://www.acme.com" elementFormDefault="unqualified">   
  <xsd:complexType name="ParameterList">
    <xsd:sequence>
      <xsd:element ref="Parameter" minOccurs="0" maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="ParameterItem">
    <xsd:sequence>
      <xsd:element name="Name" type="xsd:string" minOccurs="1" maxOccurs="1"/>
      <xsd:element name="Value" type="xsd:string" minOccurs="1" maxOccurs="1"/>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:element name="Parameters" type="ParameterList" />
  <xsd:element name="Parameter" type="ParameterItem" />  
  <xsd:element name="Profile">
    <xsd:complexType>
      <xsd:sequence>        
        <xsd:element ref="Parameters" minOccurs="1" maxOccurs="1"/>
        <xsd:element name="Parameters" type="ParameterList" minOccurs="1" maxOccurs="1"/>       
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>   
 </xsd:schema>

And output code generated by xsd2code:

using System.Collections.Generic;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace SO2_installation
{
    public class ParameterList
    {
        public ParameterList()
        {
            Parameter = new List<ParameterItem>();
        }

        public List<ParameterItem> Parameter { get; set; }
    }

    public class ParameterItem
    {
        public string Name { get; set; }

        public string Value { get; set; }
    }

    public class Profile
    {
        public Profile()
        {
            Parameters1 = new List<ParameterItem>();
            Parameters = new List<ParameterItem>();
        }

        [XmlArray(Order = 0)]
        [XmlArrayItem("Parameter", IsNullable = false)]
        public List<ParameterItem> Parameters { get; set; }

        [XmlArray("Parameters", Form = XmlSchemaForm.Unqualified, Order = 1)]
        [XmlArrayItem("Parameter", IsNullable = false)]
        public List<ParameterItem> Parameters1 { get; set; }
    }
}

The code has been simplified by R#, so it's not exact output from the xsd2code, but it shows what bothers me - why the two properties are not marked with the same attributes?

This is a problem while preparing XML file that corresponds with the XSD. It should look like this one below, which makes the preparation very error prone.

<?xml version="1.0" encoding="utf-8"?>                      
<Profile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Parameters xmlns="http://www.acme.com">    
        <Parameter xmlns="http://www.acme.com">
            <Name xmlns="">SERIALNUMBER</Name>
            <Value xmlns="">600001</Value>
        </Parameter>        
    </Parameters>       
    <Parameters1 xmlns="">  
        <Parameter xmlns="http://www.acme.com">
            <Name xmlns="">SERIALNUMBER</Name>
            <Value xmlns="">600002</Value>
        </Parameter>        
    </Parameters1>          
</Profile>  

The answer to the question: "why two properties of the same type, based on the same xsd are marked with different attributes" is not my main concern. As I cannot change XSD files (they were sent to the clients a long time ago), I need to find a way to consume XML files regardless whether their elements are marked with namespaces or not. Currently, when I pass XML with, for example:

<Parameters1 xmlns="http://www.acme.com">   

XmlSerializer will return a parsing error. It would be nice to sent XML with all parameters marked with the namespace, or without the namespace - doesn't matter as long as they would be marked consistently.

Thank you in advance for helping me with this issue.

Additional note: the example is prepared to simplify the attached code, so please don't care that Profile consists of two properties of the same kind.

1 Answers
Related