Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以父节点的类名作为前缀的类名:使用xsd.exe从XML生成CS代码_C#_Xml_Serialization_Xsd.exe - Fatal编程技术网

C# 以父节点的类名作为前缀的类名:使用xsd.exe从XML生成CS代码

C# 以父节点的类名作为前缀的类名:使用xsd.exe从XML生成CS代码,c#,xml,serialization,xsd.exe,C#,Xml,Serialization,Xsd.exe,我正在尝试使用xsd.exe从XML文件生成一个C#文件。我面临的问题是,每个类都以其父节点的类名作为前缀。因此,它会根据XML元素的深度生成很长的名称。我正在邮寄样品。 Sample.xml <?xml version="1.0" encoding="UTF-8"?> <A attrib1="100"> <B attrib2="200"> <C attrib3="300" /> </B> </A&

我正在尝试使用xsd.exe从XML文件生成一个C#文件。我面临的问题是,每个类都以其父节点的类名作为前缀。因此,它会根据XML元素的深度生成很长的名称。我正在邮寄样品。
Sample.xml

<?xml version="1.0" encoding="UTF-8"?>
<A attrib1="100">
    <B attrib2="200">
        <C attrib3="300" />
    </B>
</A>    
在提交
xsd sample.xsd/classes
命令时,我得到sample.cs如下:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.269
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=4.0.30319.1.
// 


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

    private AB[] bField;

    private string attrib1Field;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("B", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public AB[] B {
        get {
            return this.bField;
        }
        set {
            this.bField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string attrib1 {
        get {
            return this.attrib1Field;
        }
        set {
            this.attrib1Field = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class AB {

    private ABC[] cField;

    private string attrib2Field;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("C", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public ABC[] C {
        get {
            return this.cField;
        }
        set {
            this.cField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string attrib2 {
        get {
            return this.attrib2Field;
        }
        set {
            this.attrib2Field = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class ABC {

    private string attrib3Field;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string attrib3 {
        get {
            return this.attrib3Field;
        }
        set {
            this.attrib3Field = value;
        }
    }
}

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

    private A[] itemsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("A")]
    public A[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }
}
//------------------------------------------------------------------------------
// 
//这段代码是由一个工具生成的。
//运行时版本:4.0.30319.269
//
//对此文件的更改可能会导致不正确的行为,如果
//重新生成代码。
// 
//------------------------------------------------------------------------------
使用System.Xml.Serialization;
// 
//此源代码由xsd自动生成,版本=4.0.30319.1。
// 
/// 
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”,“4.0.30319.1”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace=”“,IsNullable=false)]
公共部分A类{
私人机场;
私有字符串attrib1Field;
/// 
[System.Xml.Serialization.xmlementAttribute(“B”,Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
公共AB[]B{
得到{
返回此.bField;
}
设置{
this.bField=值;
}
}
/// 
[System.Xml.Serialization.XmlAttributeAttribute()]
公共字符串attrib1{
得到{
返回此.attrib1Field;
}
设置{
this.attrib1Field=值;
}
}
}
/// 
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”,“4.0.30319.1”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
公共部分AB类{
私人农场;
私有字符串attrib2Field;
/// 
[System.Xml.Serialization.xmlementAttribute(“C”,Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
公共广播{
得到{
返回此.cField;
}
设置{
this.cField=值;
}
}
/// 
[System.Xml.Serialization.XmlAttributeAttribute()]
公共字符串attrib2{
得到{
返回this.attrib2Field;
}
设置{
this.attrib2Field=值;
}
}
}
/// 
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”,“4.0.30319.1”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
公共部分类ABC{
私有字符串attrib3Field;
/// 
[System.Xml.Serialization.XmlAttributeAttribute()]
公共字符串attrib3{
得到{
返回此.attrib3字段;
}
设置{
this.attrib3Field=值;
}
}
}
/// 
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”,“4.0.30319.1”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace=”“,IsNullable=false)]
公共部分类NewDataSet{
私有A[]项字段;
/// 
[System.Xml.Serialization.XmlElementAttribute(“A”)]
公共A[]项目{
得到{
返回此.itemsField;
}
设置{
this.itemsField=值;
}
}
}

问题是它正在生成类ABABC,它们分别在类A和类AB中使用。这是在真实场景中创建很长的名称。有没有办法抑制这种行为,这样我就可以将类设置为a、B和C,而不是a、AB和ABC?请使用我的示例作为参考。

您需要给出所有复杂类型的名称和定义 在模式中显式,然后在 定义元素。XSD.exe工具将使用这些“类型” 名称作为类名。下面是A的一个示例。您需要中断 在模式中以类似方式输出所有复杂类型

<xs:element name="A" type="AType"/>

<xs:complexType name="AType">
    <xs:sequence>
        <xs:element name="B" type="BType" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    <xs:attribute name="attrib1" type="xs:string"/>
</xs:complexType>

也欢迎任何可以完成此任务的替代工具。
<xs:element name="A" type="AType"/>

<xs:complexType name="AType">
    <xs:sequence>
        <xs:element name="B" type="BType" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    <xs:attribute name="attrib1" type="xs:string"/>
</xs:complexType>