代码之家  ›  专栏  ›  技术社区  ›  Ben Hymers

xsd.exe在OFX2架构上运行时会生成重复的属性

  •  2
  • Ben Hymers  · 技术社区  · 17 年前

    使用命令行:

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

    生成的C#源文件无法生成,出现以下错误:

    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
    

    我从OFX2模式复制了一个类似的XSD模式,然后将其精简到我想要的有用位,生成了一个C#文件,该文件构建得很好,但与完整模式的C#表示具有所有相同的属性。

    知道为什么吗?OFX模式被破坏了吗?xsd.exe是否已损坏?C#坏了吗?我破产了吗?

    3 回复  |  直到 17 年前
        1
  •  7
  •   Chris Cudmore    15 年前

    好的,这个答案是很长一段时间来。。。

    我刚刚遇到了同样的问题。问题不在foo.cs中,而是在foo.designer.cs中。您必须删除第二个类中的重复属性。

    C#应该允许跨分部类复制属性,或者修复xsd以忽略除.cs文件之外的所有属性。

        2
  •  2
  •   etwas77    13 年前

    对于不同的模式,我遇到了相同的问题(相同的“重复属性”问题)。原因是因为有两个xsd模式(两个生成的文件),在每个模式中我都有相同的元素“类型”,但定义不同。将其中一个类型重命名为不同的名称解决了此问题

        3
  •  1
  •   Anthony    15 年前

        4
  •  0
  •   Sonny N    6 年前

    我自己也遇到过这个问题,结果发现对于那些我得到重复属性错误的类,它们已经在同一名称空间的其他地方声明了。因此,任何试图排除故障的人,都需要确保在给定名称空间下只声明一次罪犯类。例如,XSD.exe导入生成了以下类:

    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;
                }
            }
        }
    }
    

    由于XSD.exe将所有导入的类型声明为分部类,并且由于其他早期XSD导入,DocType已存在于同一命名空间中,因此在DocType类上出现重复属性错误。我只是将它的名称空间更改为Example.Imports.Orders,因此此名称空间下只有DocType。我希望这说明了问题和可能的解决方法。