3

Using the command line:

"xsd.exe" "OFX 2.1.1 schema/OFX2_Protocol.xsd" /c /namespace:OFX /nologo"

The resulting C# source file fails to build with these errors:

D:\blah\OFX2_Protocol.cs(19,6): error CS0579: Duplicate 'System.CodeDom.Compiler.GeneratedCodeAttribute' attribute
D:\blah\OFX2_Protocol.cs(20,6): error CS0579: Duplicate 'System.SerializableAttribute' attribute
D:\blah\OFX2_Protocol.cs(21,6): error CS0579: Duplicate 'System.Diagnostics.DebuggerStepThroughAttribute' attribute
D:\blah\OFX2_Protocol.cs(22,6): error CS0579: Duplicate 'System.ComponentModel.DesignerCategoryAttribute' attribute
D:\blah\OFX2_Protocol.cs(23,6): error CS0579: Duplicate 'System.Xml.Serialization.XmlTypeAttribute' attribute
D:\blah\OFX2_Protocol.cs(24,6): error CS0579: Duplicate 'System.Xml.Serialization.XmlRootAttribute' attribute

A similar XSD schema, which I copied from the OFX2 schema then trimmed down to the useful bits that I wanted, generates a C# file which builds just fine, yet has all the same attributes as the full schema's C# representation.

Any idea why? Is the OFX schema broken? Is xsd.exe broken? Is C# broken? Am I broken?

Ben Hymers
  • 25,586
  • 16
  • 59
  • 84

4 Answers4

7

Ok, this answer is a long time coming...

I just ran into the same issue. The problem wasn't in foo.cs, but in foo.designer.cs. You have to remove the duplicate attributes in the second class.

C# should either allow duplicate attributes accross partial classes, or fix xsd to omit attributes in all but the .cs file.

Chris Cudmore
  • 29,793
  • 12
  • 57
  • 94
  • Just noticed this answer - whilst "fix xsd" isn't really feasible it's definitely correct :) The behaviour is clearly wrong and I don't think it's our fault. – Ben Hymers Dec 18 '12 at 12:14
  • I agree. Something is broken, and it's not our fault. – Chris Cudmore Jan 07 '13 at 15:46
  • Thanks. In Visual Studio, you can edit the Properties of the xsd file and set "Custom Tool" to blank if you don't want to generate the designer file at all. – default.kramer Dec 02 '15 at 18:07
3

i had the same issue (the same "duplicate attributes" problem) with different schemas. the reason was due to 2 xsd schemas (2 generated files) and in each of them i had the same "type" of element, but with different definitions. renaming of one of the types into different name solved the problem

etwas77
  • 514
  • 1
  • 6
  • 17
1

The latest version of the OFX specification download has an 'OFX3_Protocol_dotNET.xsd' which has been modified from the 'OFX2_Protocol.xsd' to be more suited to .NET code generation tools. I have generated C# from this xsd without any problems although I haven't deserialised any XML yet.

Anthony
  • 1,706
  • 2
  • 22
  • 46
0

Just ran into this myself and it turns out that for those classes that I was getting the duplicate attribute error are already declared elsewhere under the same namespace. So, anyone that is trying to troubleshoot, you want to make sure the culprit classes are declared only once under a given namespace. For example, XSD.exe import generated the following class:

namespace Example.Imports 
{
  using System.Xml.Serialization;

    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.example.com/Common")]
    [System.Xml.Serialization.XmlRootAttribute("order", Namespace="http://www.example.com/Order", IsNullable=false)]
    public partial class OrderType {
    
        private DocType docField;
        
        public DocType doc {
            get {
                return this.docField;
            }
            set {
                this.docField = value;
            }
        }
    }
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.example.com/Common")]
    public partial class DocType {
        
        private System.Xml.XmlElement[] anyField;
        
        /// <remarks/>
        [System.Xml.Serialization.XmlAnyElementAttribute()]
        public System.Xml.XmlElement[] Any {
            get {
                return this.anyField;
            }
            set {
                this.anyField = value;
            }
        }
    }
}

I was getting duplicate attribute error on DocType class, since XSD.exe declares all imported types as partial classes and DocType was already exist in the same namespace due to other earlier XSD imports. I simply changed the namespace for it to Example.Imports.Orders so there is only DocType under this namespace. I hope this illustrates the problem and possible fixes.

Sonny N
  • 170
  • 8