简体   繁体   中英

Xsd.exe generates buggy implementation of <xs:list>

This is an example of what Xsd.exe generates (also Xsd2Code but using generic List<>).

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="xxxxxxxxxxxxxxxx")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="xxxxxxxxxxxxxxxx", IsNullable=false)]
public partial class ItemQuantity 
{

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Units;

    /// <remarks/>
    [System.Xml.Serialization.XmlAnyAttributeAttribute()]
    public System.Xml.XmlAttribute[] AnyAttr;

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()] // <-- this is the bug
    public double[] Text;   // <-- this is the bug corresponding to a <xs:list itemType=xs:double/> in the schema
}

I am dealing with a very large schema, which uses several times xs:list for different primitives...

And all these Text properties that are not string[] (rather double[] decimal[]) all lead to this exception at run-time when used with XmlSerializer : (I translate it in english) The member Text cannot be coded with the attribute XmlText. You can use the attribute XmlText with primitives, enums, string arrays, or XmlNode arrays.

What do you advise me ?

I have edited the xsd.exe generated code and put

[System.Xml.Serialization.XmlArrayItemAttribute("Double",typeof(double), IsNullable = false)]

instead of

[System.Xml.Serialization.XmlTextAttribute()]

and as far as I can judge... it does behaves much better....

ie

<Test><Double>0.1</Double><Double>0.2</Double><Double>0.8</Double><Double>0.505</Double></Test>

But I should ensure that enclosing all that list in the Test tag is still valid with my schema...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM