以父节点的类名为前缀的类名:使用xsd.exe从XML生成CS代码
本文关键字:XML 生成 CS exe 代码 父节点 前缀 使用 xsd | 更新日期: 2023-09-27 18:20:19
我正试图使用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和类1AB、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>