我正在尝试使用 xsd.exe 从 XML 文件生成 C# 文件。我面临一个问题,每个类都以其父节点的类名作为前缀。因此,它会生成很长的名称,具体取决于 XML 元素的深度。我正在发布一个样本。
示例.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?请以我的例子作为参考。