我需要使用XML文件填充類。使用LINQ查詢填充類
<Ship>
<Name>Base Ship</Name>
<Owner>PG</Owner>
<Aim>
<Type>base</Type>
<Value>10</Value>
<Last>-1</Last>
</Aim>
<Aim>
<Type>cannon</Type>
<Value>10</Value>
<Last>2</Last>
</Aim>
<Dodge>
<Type>base</Type>
<Value>10</Value>
<Last>-1</Last>
</Dodge>
<EmPower>
<Type>base</Type>
<Value>10</Value>
<Last>-1</Last>
</EmPower>
</Ship>
我的問題是如何來填充Dictionary<string, CustomStruct>
這是struct
:
public struct Stat
{
public int StatValue { get; set; }
public int StatLast { get; set; }
public Stat(int statValue, int statLast)
{
StatValue = statValue;
StatLast = statLast;
}
}
我的LINQ查詢看起來是這樣的:
string loadDataPath = Application.persistentDataPath + "/saveData.xml";
XDocument loadData = XDocument.Load(loadDataPath);
var query = from item in loadData.Elements("Ship")
select new Ship()
{
Name = (string) item.Element("Name"),
Owner = (string) item.Element("Owner"),
Aim = item.Elements("Aim") // <-- Here lies the problem.
// ...
};
對於每個目標XElements
我需要使用t填充Aim字典他下面的方法:
Aim TKey = XML Type
Aim TValue.StatValue = XML Value
Aim TValue.StatLast = XML Last
如果你看看我的更新答案,你就會知道爲什麼'struct'沒有工作。 –