我需要阅读以下XML docuemt并保存变量中的所有字段以供日后使用。 试过Linq但是真的不明白如何使用它。 我需要帮助,我应该编写什么代码来读取XML并将字段保存在变量中?
<?xml version="1.0" encoding="utf-8"?>
<Map name="Mapa Prueba" width="30" height="30" hp="500" orobyte="50" Tx="1" Ty="5" Sx="30" Sy="25" >
<StructuresList>
<Structure name="Block de Notas" attack="0" i="1" j="1" range="0" price="8" />
<Structure name="Excel" attack="2" i="2" j="2" range="1" price="20" />
<Structure name="Word" attack="3" i="2" j="2" range="1" price="40" />
<Structure name="PowerPoint" attack="3" i="3" j="2" range="1" price="50" />
<Structure name="FireWall" attack="1" i="1" j="1" range="1" price="60" />
<Structure name="Avast" attack="4" i="3" j="3" range="2" price="80" />
<Structure name="Nod32" attack="4" i="3" j="2" range="2" price="110" />
<Structure name="Panda" attack="6" i="3" j="2" range="1" price="130" />
<Structure name="Norton" attack="5" i="3" j="3" range="2" price="150" />
<Structure name="Eclipse" attack="4" i="3" j="4" range="3" price="180" />
<Structure name="Visual Studio" attack="10" i="5" j="6" range="1" price="200" />
</StructuresList>
<EnemysList>
<Enemy id="00" color="rojo" hp="20" orobyte="1" speed="1" />
<Enemy id="01" color="azul" hp="10" orobyte="1" speed="2" />
<Enemy id="02" color="amarillo" hp="35" orobyte="3" speed="1" />
<Enemy id="03" color="celeste" hp="30" orobyte="2" speed="2" />
<Enemy id="04" color="azul" hp="10" orobyte="1" speed="4" />
<Enemy id="05" color="rojo" hp="60" orobyte="3" speed="1" />
<Enemy id="06" color="celeste" hp="100" orobyte="10" speed="1" />
<Enemy id="07" color="amarillo" hp="150" orobyte="25" speed="3" />
<Enemy id="08" color="verde" hp="200" orobyte="40" speed="5" />
</EnemysList>
<Waves>
<Wave>
<Group id="00" count="10" />
<Group id="01" count="5" />
</Wave>
<Wave>
<Group id="00" count="15" />
<Group id="01" count="5" />
</Wave>
<Wave>
<Group id="01" count="25" />
</Wave>
<Wave>
<Group id="00" count="24" />
<Group id="01" count="5" />
</Wave>
<Wave>
<Group id="02" count="20" />
</Wave>
<Wave>
<Group id="02" count="10" />
<Group id="03" count="20" />
</Wave>
<Wave>
<Group id="03" count="20" />
<Group id="07" count="1" />
</Wave>
<Wave>
<Group id="03" count="10" />
<Group id="04" count="30" />
</Wave>
<Wave>
<Group id="05" count="30" />
<Group id="06" count="10" />
<Group id="07" count="1" />
</Wave>
<Wave>
<Group id="08" count="3" />
</Wave>
</Waves>
</Map>
答案 0 :(得分:1)
答案 1 :(得分:0)
如果您想使用linq
,这是一个基本示例。例如,使用这段代码,您将检索Structure
中的所有StructureList
并检查属性。这很容易,你会有一个想法。
var xDoc = XDocument.Load(xmlFile); //Or XDocument.Parse(xmlString)
var structures = xDoc.Descendants("StructuresList").Descendants("Structure")
.ToList();
foreach (var structure in structures)
{
var name = structure.Attribute("name").Value;
var attack = structure.Attribute("attack").Value;
//Continue checking attributes
}
答案 2 :(得分:0)
static void Main(string[] args)
{
string path =@"file.xml";
Map mymap = new Map();
var xml = XDocument.Load(path);
var map = xml.Descendants("Map").Select(x => new Map()
{
Structures = x.Element("StructuresList").Elements("Structure").Select(r => new StructuresList()
{
Name = r.Attribute("name").Value,
Attack =Convert.ToInt32(r.Attribute("attack").Value),
I = Convert.ToInt32(r.Attribute("i").Value),
J = Convert.ToInt32(r.Attribute("j").Value),
Range = Convert.ToInt32(r.Attribute("range").Value),
Price = Convert.ToInt32(r.Attribute("price").Value)
}).ToList(),
Enemys = x.Element("EnemysList").Elements("Enemy").Select(r => new EnemysList()
{
ID = Convert.ToInt32(r.Attribute("id").Value),
Color = r.Attribute("color").Value.ToString(),
HP = Convert.ToInt32(r.Attribute("hp").Value),
Orobyte = Convert.ToInt32(r.Attribute("orobyte").Value),
Speed = Convert.ToInt32(r.Attribute("speed").Value)
}).ToList(),
Waves = x.Element("Waves").Elements("Wave").Select(y => new Wave()
{
Groups = y.Elements("Group").Select(r => new Group()
{
ID = Convert.ToInt32(r.Attribute("id").Value),
Count = Convert.ToInt32(r.Attribute("count").Value)
}).ToList()
}).ToList()
}).First();
}
internal class Map
{
public List<StructuresList> Structures { set; get; }
public List<EnemysList> Enemys { set; get; }
public List<Wave> Waves { set; get; }
}
internal class StructuresList
{
public string Name{set;get;}
public int Attack { set; get; }
public int I { set; get; }
public int J { set; get; }
public int Range { set; get; }
public int Price { set; get; }
}
internal class EnemysList
{
public int ID { set; get; }
public string Color { set; get; }
public int HP { set; get; }
public int Orobyte { set; get; }
public int Speed { set; get; }
}
internal class Wave
{
public int WaveID { set; get; }
public List<Group> Groups { set; get; }
}
internal class Group
{
public int ID { set; get; }
public int Count { set; get; }
}
打印数据
Console.WriteLine("Strucrures");
foreach (var str in map.Structures)
{
Console.WriteLine("Name :{0} , Attack:{1} , I:{2} , J:{3} , Range:{4} , Price:{5}"
, str.Name, str.Attack, str.I, str.J, str.Range, str.Price);
}
Console.WriteLine("Enemys");
foreach (var enmy in map.Enemys)
{
Console.WriteLine("ID :{0} , Color:{1} , HP:{2} , Orobyte:{3} , Speed:{4} "
, enmy.ID, enmy.Color, enmy.HP, enmy.Orobyte, enmy.Speed);
}
Console.WriteLine("Waves");
foreach (var enmy in map.Waves)
{
Console.WriteLine("Wave ID:{0}", enmy.WaveID);
foreach(var gr in enmy.Groups)
Console.WriteLine("ID :{0} , Count:{1} "
, gr.ID, gr.Count);
}