I cannot understand why C # code generators (xsd, xsd2code) create an output profile class with two properties of the same type, marked with different attributes. One of them is marked as unskilled, and the other 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 the 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 was simplified by R #, so it is not the exact output from xsd2code, but it shows what bothers me - why are the two properties not marked with the same attributes?
This is a problem when preparing an XML file that corresponds to XSD. It should look like the 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 are two properties of the same type, based on the same xsd, marked with different attributes”, is not my main task. Since I cannot change the XSD files (they were sent to clients a long time ago), I need to find a way to use the XML files, regardless of whether their elements are marked with a namespace or not. Currently, when I pass XML, for example:
<Parameters1 xmlns="http://www.acme.com">
XmlSerializer will return a parsing error. It would be nice to send XML with all parameters marked with a namespace, or without a namespace - it doesn't matter if they are sequentially marked.
Thank you in advance for your help in resolving this issue.
Additional note: the example is prepared to simplify the attached code, so please do not care that the profile consists of two properties of the same type.