简体   繁体   English

使用XSD.exe生成的这些类级别属性中的哪一个可以安全地丢弃?

[英]Which of these class level attributes generated by using XSD.exe can be discarded safely?

I used XSD.exe against an XML schema file ( .XSD ) to generate C# classes for use in deserializing XML data files that conform to the said XML schema. 我对XML模式文件( .XSD )使用XSD.exe生成C#类,用于反序列化符合所述XML模式的XML数据文件。

I noticed the following class level attributes have been added to all the generated classes. 我注意到以下类级别属性已添加到所有生成的类中。

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]

In some cases, the tool has also generated the XmlElementAttribute attribute, which I am well aware is necessary for proper deserialization. 在某些情况下,该工具还生成了XmlElementAttribute属性,我知道该属性对于正确的反序列化是必需的。

[System.Xml.Serialization.XmlElementAttribute("Field")]

So other than this XmlElementAttribute , which of the aforementioned attributes are really required for deserialization to be successful. 因此,除了XmlElementAttribute ,要成功实现反序列化,实际上还需要上述哪些属性。 I know I can remove them 1 by 1 and try it out, but there are a lot of classes and I want to keep my class definitions as 'clean' as possible without having un-necessary attributes all over the place. 我知道我可以将它们一对一地删除并尝试一下,但是有很多类,并且我想让我的类定义尽可能保持“干净”,而不必在所有地方使用不必要的属性。

Here's a sample class definition generated by XSD.exe , in case you need to look at the class also. 这是XSD.exe生成的示例类定义,以防您还需要查看该类。

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class CrystalReportReportFooter {

    private CrystalReportReportFooterSection sectionField;

    /// <remarks/>
    public CrystalReportReportFooterSection Section {
        get {
            return this.sectionField;
        }
        set {
            this.sectionField = value;
        }
    }
}

Thank you, 谢谢,

Should be safe to remove unless the tool that regenerates the code needs this. 除非重新生成代码的工具需要此工具,否则应安全删除。

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]

You need this as this is what tells a class that it can be serialized. 您需要它,因为这告诉了类可以序列化。

[System.SerializableAttribute()]

This can be removed. 可以将其删除。 It's purely for debugging and may end up hindering debugging later on if you add custom code. 它仅用于调试,如果添加自定义代码,可能最终会阻碍以后的调试。

[System.Diagnostics.DebuggerStepThroughAttribute()]

This can be removed. 可以将其删除。 Only used for the IDE or any WYSIWYG designer that takes advantage of it. 仅用于IDE或任何利用它的WYSIWYG设计器。

[System.ComponentModel.DesignerCategoryAttribute("code")]

This is up to you. 这取决于你。 Basically it is telling the Serializer that the class is an anonymous XSD type. 基本上,它告诉串行器该类是匿名XSD类型。

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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