vs附带小工具:xsd
https://docs.microsoft.com/en-us/dotnet/standard/serialization/xml-schema-definition-tool-xsd-exe
https://stackoverflow.com/questions/4203540/generate-c-sharp-class-from-xml
https://www.c-sharpcorner.com/UploadFile/pranayamr/generate-class-from-json-xml-in-visual-studio/
网页小工具:
https://www.liquid-technologies.com/online-xml-to-xsd-converter
思路:
1.xml转xsd:
PS C:Program Files (x86)Microsoft SDKsWindowsv10.0AinNETFX 4.8 Tools> .xsd.exe .11111请求报文.xml -outputdir:.11111 Microsoft(R) Xml 架构/数据类型支持实用工具 [Microsoft (R) .NET Framework, Version 4.8.3928.0] Copyright (C) Microsoft Corporation. All rights reserved. 正在写入文件“.11111请 求报文.xsd”。
2.xsd转class
PS C:Program Files (x86)Microsoft SDKsWindowsv10.0AinNETFX 4.8 Tools> .xsd.exe .11111请求报文.xsd /classes -outputdir:.11111 Microsoft(R) Xml 架构/数据类型支持实用工具 [Microsoft (R) .NET Framework, Version 4.8.3928.0] Copyright (C) Microsoft Corporation. All rights reserved. 正在写入文件“.11111请 求报文.cs”。 PS C:Program Files (x86)Microsoft SDKsWindowsv10.0AinNETFX 4.8 Tools>
成功。
原始xml
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Request" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Request" msdata:IsDataSet="true" msdata:Locale="en-US">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Header">
<xs:complexType>
<xs:sequence>
<xs:element name="TransDateTime" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Body">
<xs:complexType>
<xs:sequence>
<xs:element name="BusRelTable" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="City" type="xs:string" minOccurs="0" />
<xs:complexType>
<xs:sequence>
<xs:element name="ContactPersonTable" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="FirstName" type="xs:string" minOccurs="0" />
<xs:element name="MiddleName" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
中间结果,xsd:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Request" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Request" msdata:IsDataSet="true" msdata:Locale="en-US">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Header">
<xs:complexType>
<xs:sequence>
<xs:element name="TransDateTime" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Body">
<xs:complexType>
<xs:sequence>
<xs:element name="BusRelTable" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="City" type="xs:string" minOccurs="0" />
<xs:complexType>
<xs:sequence>
<xs:element name="ContactPersonTable" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="FirstName" type="xs:string" minOccurs="0" />
<xs:element name="MiddleName" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
最终class
//------------------------------------------------------------------------------
// <auto-generated>
// 此代码由工具生成。
// 运行时版本:4.0.30319.42000
//
// 对此文件的更改可能会导致不正确的行为,并且如果
// 重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// 此源代码由 xsd 自动生成, Version=4.8.3928.0。
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
[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 Request {
private object[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Body", typeof(RequestBody), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("Header", typeof(RequestHeader), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public object[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class RequestBody {
private RequestBodyBusRelTable[] busRelTableField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("BusRelTable", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public RequestBodyBusRelTable[] BusRelTable {
get {
return this.busRelTableField;
}
set {
this.busRelTableField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class RequestBodyBusRelTable {
private string cityField;
private RequestBodyBusRelTableContactPersonListContactPersonTable[][] contactPersonListField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string City {
get {
return this.cityField;
}
set {
this.cityField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class RequestBodyBusRelTableContactPersonListContactPersonTable {
private string firstNameField;
private string middleNameField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string FirstName {
get {
return this.firstNameField;
}
set {
this.firstNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string MiddleName {
get {
return this.middleNameField;
}
set {
this.middleNameField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class RequestHeader {
private string transDateTimeField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string TransDateTime {
get {
return this.transDateTimeField;
}
set {
this.transDateTimeField = value;
}
}
}