我正在尝试使用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.xml
命令后,我得到 Sample.xsd ,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="A">
<xs:complexType>
<xs:sequence>
<xs:element name="B" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="C" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="attrib3" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="attrib2" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="attrib1" type="xs:string" />
</xs:complexType>
</xs:element>
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="A" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
在提交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;
}
}
}
问题是它产生了 AB 和 ABC 类,它们正在课程 A 和课程 AB中使用分别。这在真实场景中创建了很长的名称。有没有办法抑制这种行为,所以我得到的课程是 A,B和C ,而不是 A,AB和ABC ?请以我的例子为参考。
答案 0 :(得分:2)
您需要为所有复杂类型指定名称和定义 在架构中显式,然后在引用这些类型时 定义元素。 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>