我需要使用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'没有工作。 –