您可以使用XSD.exe生成类,我认为它是Visual Studio的一部分。
如果打开Visual Studio命令提示符,您可以访问它。
在硬盘的某个地方创建一个物理XML文件。 (我用了你的XML和装箱sample.xml中)
打开Visual Studio命令提示符
导航到该目录
型 'XSD sample.xml中'(不带引号),这将生成(没有引号)在 'XSD sample.xsd/C' sample.xsd
类型,这将产生sample.cs
设置使用Microsoft Visual Studio 2008 x86工具的环境。
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC>G:
G:\>xsd sample.xml
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.3038]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'G:\sample.xsd'.
G:\>xsd sample.xsd /c
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.3038]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'G:\sample.cs'.
G:\>
生成:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="chart" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="value" nillable="true">
<xs:complexType>
<xs:simpleContent msdata:ColumnName="value_Text" msdata:Ordinal="1">
<xs:extension base="xs:string">
<xs:attribute name="xid" type="xs:string" />
<xs:attribute name="color" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="chart" msdata:IsDataSet="true" msdata:Locale="en-US">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="value" />
<xs:element name="series">
<xs:complexType>
<xs:sequence>
<xs:element ref="value" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="graphs">
<xs:complexType>
<xs:sequence>
<xs:element name="graph" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element ref="value" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="gid" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
和样品。cs
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.5446
//
// 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=2.0.50727.3038.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=true)]
public partial class value {
private string xidField;
private string colorField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string xid {
get {
return this.xidField;
}
set {
this.xidField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string color {
get {
return this.colorField;
}
set {
this.colorField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value {
get {
return this.valueField;
}
set {
this.valueField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[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 chart {
private object[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("graphs", typeof(chartGraphs), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("series", typeof(value[]), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("value", typeof(value), IsNullable=true)]
public object[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class chartGraphs {
private chartGraphsGraph[] graphField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("graph", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public chartGraphsGraph[] graph {
get {
return this.graphField;
}
set {
this.graphField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class chartGraphsGraph {
private value[] valueField;
private string gidField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("value", IsNullable=true)]
public value[] value {
get {
return this.valueField;
}
set {
this.valueField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string gid {
get {
return this.gidField;
}
set {
this.gidField = value;
}
}
}
这有点麻烦,但为您节省了大量的时间和精力。
*“不完全工作(实际上,其中的一部分显然是错误的)”* - **什么**不起作用? **“什么**是”显然是错的?“ – cdhowie
为什么不从xml生成xsd,然后使用xsd生成类。 xsd.exe是你的朋友。 – Phill