VS 2012 で Xsd2Code を使用して、xsd ファイルからクラスを生成します。まず、次の XML から xsd ファイルを作成します。
<?xml version="1.0" encoding="utf-8" ?>
<Students>
<Student>
<RollNo>1</RollNo>
<Name>Student 1</Name>
<Address>Xyz Street</Address>
</Student>
</Students>
次に、結果 (xsd ファイル) を Xsd2Code ツールに使用し、次のクラスを取得します。それらを使用するには、 Attribute を追加する必要があります[XmlElement("Student")]
。クラスファイルを編集しなくても出力が生成されるように、Xsd2Codeメニューにいくつかの設定がありますか?
namespace ConsoleApplication2
{
using System;
using System.Diagnostics;
using System.Xml.Serialization;
using System.Collections;
using System.Xml.Schema;
using System.ComponentModel;
using System.Collections.Generic;
using MongoDB.Bson.Serialization.Attributes;
public partial class Students{
private List<StudentsStudent> studentField;
public Students(){
this.studentField = new List<StudentsStudent>();
}
[XmlElement("Student")]
public List<StudentsStudent> Student{
get{return this.studentField;}
set{this.studentField = value;}
}
}
public partial class StudentsStudent{
private byte rollNoField;
private string nameField;
private string addressField;
public byte RollNo{
get{return this.rollNoField;}
set{this.rollNoField = value;}
}
public string Name{
get{return this.nameField;}
set{this.nameField = value;}
}
public string Address{
get{return this.addressField;}
set{this.addressField = value;}
}
}
}