コンテンツパイプラインを使ったXML読み込み
■XMLファイルの作成
コンテンツディレクトリに「XMLtest.xml」を以下の内容で作成します。
AssetNmaeは今回は"XMLtest"にします。
<?xml version="1.0" encoding="utf-8"?> <XnaContent xmlns:Generic="System.Collections.Generic"> <Asset Type="Generic:Dictionary[string,int]"> <Item> <Key>World01</Key> <Value>54</Value> </Item> <Item> <Key>World02</Key> <Value>300</Value> </Item> </Asset> </XnaContent>
■読み込み
読み込みは実にシンプルで
Dictionary<string, int> param = Content.Load<Dictionary<string, int>>("XMLtest");
でparam構造体にデータが入っています。
ウオッチリストで中身を確認すると
■データの使用。
foreach (string sKey in param.Keys) { int iValu; iValu = param[sKey]; }
sKeyに"World01","World02"というデータが入ってきます。
iValu = param[sKey];
でiValuには"World01"の場合は54、"World02"の場合は300が取得できます。
■その他
XMLファイル内で
<Asset Type="Generic:Dictionary[string,int]">
と定義されています、データ読み込み時、
Dictionary<string, int>
と指定しているので読み込めますが、型が異なるとファイルが見つからないと怒られます。