前回は自分の作成したクラスを使ってXMLファイルから読み書きしました。
XMLファイル・XSDファイルがあってこれを読み込みたい場合があると思います。
XMLファイル・XSDファイルからクラスを作れれば前回のやり方が使えます。
まずXMLファイルからXSDファイルを作りましょう。
コマンドプロンプトを起動します。
(Visual Studio .NETフォルダ)¥Common7¥Tools にある vsvars32.bat を実行します。
xsd XMLファイル名 を実行します。
たとえばこんな感じのXSDファイルができます。
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="opml" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="opml" msdata:IsDataSet="true" msdata:Locale="ja-JP">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="body">
<xs:complexType>
<xs:sequence>
<xs:element name="outline" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="text" type="xs:string" />
<xs:attribute name="htmlUrl" type="xs:string" />
<xs:attribute name="xmlUrl" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
xsd XSDファイル名 /c を実行します。
先ほどのXSDファイルからはこんな感じのCSファイルができます。
//------------------------------------------------------------------------------
// <autogenerated>
// This code was generated by a tool.
// Runtime Version: 1.1.4322.573
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </autogenerated>
//------------------------------------------------------------------------------
//
// このソース コードは xsd によって自動生成されました。Version=1.1.4322.573 です。
//
using System.Xml.Serialization;
/// <remarks/>
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public class opml {
/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute("body", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("outline", typeof(opmlBodyOutline), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
public opmlBodyOutline[][] Items;
}
/// <remarks/>
public class opmlBodyOutline {
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string text;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string htmlUrl;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string xmlUrl;
}