I have this sample XML file:
<?xml version="1.0"?>
<DocumentElement>
<item>
<id>1</id>
<name>BBB</name>
</item>
<item>
<id>2</id>
<name>BBB</name>
</item>
</DocumentElement>
I want to generate XSD schema using xsd.exe utility and run this command:
xsd.exe myXmlFile.xml
This is what I got on output:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="DocumentElement" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="DocumentElement" msdata:IsDataSet="true" msdata:Locale="en-US">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="item">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:string" minOccurs="0" />
<xs:element name="name" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
Notice both fields are string but I expected them to be an integer and a string.
If I generate schema in VS then I get a better result:
<xs:element name="id" type="xs:unsignedByte" />
<xs:element name="name" type="xs:string" />
How can I make xsd.exe work like in VS?
You can try the following steps to generate xsd files with strong types.
First, please generate a xsd file by using the command:
xsd D:\Sample.xml /outputdir:D:\
Second , please generate a class from the xsd file:
xsd D:\Sample.xsd /classes /outputdir:D:\
Third , please create a class library(.net framework 3.5) and add the generated class to the project, rebuild it.
Fourth , please change string type to int type about id.
public partial class DocumentElementItem {
private int idField; //Change string to int
private string nameField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public int id { //Change string to int
get {
return this.idField;
}
set {
this.idField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
}
Finally , please use the following command to generate a xsd file with strong types.
xsd "D:\Test\bin\Debug\Test.dll" /type:DocumentElementItem /outputdir:D:\
You will get the following xsd file.
Besides, you can see all the processes in the following picture.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.