xsdによるクラスの自動生成

Visual studio に付属しているxsdツールは、xmlからxsd (xml schema)、xsdからc#,vb.netのクラスを自動生成することができます。
xmlファイルを読み込もうとしている場合、xsdやC#,vb.Netのクラスを手書きするのではなく、xsdツールを使って生成すると便利です。
生成されたxsdやC#,vb.Netのクラスは、不要な部分や厳密でない部分を訂正する箇所もあります。
ですが、最初xsdツールで生成して手修正を加えるという方が楽に作業ができます。

ツールの使い方


c:\xml>xsd test.xml
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.3038]
Copyright (C) Microsoft Corporation. All rights reserved.
ファイル 'c:\xml\test.xsd' に書き込んでいます。

c:\xml>xsd /l:cs /c test.xsd
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.3038]
Copyright (C) Microsoft Corporation. All rights reserved.
ファイル 'c:\xml\test.cs' に書き込んでいます。

xml


taro kobe
manager


■xsd


















c#
//------------------------------------------------------------------------------
//
// このコードはツールによって生成されました。
// ランタイム バージョン:2.0.50727.4016
//
// このファイルへの変更は、以下の状況下で不正な動作の原因になったり、
// コードが再生成されるときに損失したりします。
//

//------------------------------------------------------------------------------

using System.Xml.Serialization;

//
// このソース コードは xsd によって自動生成されました。Version=2.0.50727.3038 です。
//


///
[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 item {

private string nameField;

private string jobField;

private string idField;

///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}

///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string job {
get {
return this.jobField;
}
set {
this.jobField = value;
}
}

///
[System.Xml.Serialization.XmlAttributeAttribute()]
public string id {
get {
return this.idField;
}
set {
this.idField = value;
}
}
}

///
[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 NewDataSet {

private item[] itemsField;

///
[System.Xml.Serialization.XmlElementAttribute("item")]
public item[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}