my problem is how to implement a linq query that parse from an XML file to a List class Here is the custom class:
CustomClass.cs
public class ListItem
{
public int id;
public string name;
public int minLevel;
public int minAttr1;
public int minAttr2;
public float damage;
public float additionalDmg;
public string additionalDmgType;
public float range;
public float cost;
public ListItem(
int _id,
string _name,
int _minLevel,
int _minAttr1,
int _minAttr2,
float _damage,
float _additionalDmg,
string _additionalDmgType,
float _range,
float _cost)
{
id = _id;
name = _name;
minLevel = _minLevel;
minAttr1 = _minAttr1;
minAttr2 = _minAttr2;
damage = _damage;
additionalDmg = _additionalDmg;
additionalDmgType = _additionalDmgType;
range = _range;
cost = _cost;
}
}
This is an example of the xml file that I have:
File.xml
<?xml version="1.0" encoding="utf-8"?>
<Weapons>
<Weapon id="0">
<Name>spada</Name>
<MinLevel>1</MinLevel>
<MinAttr1>10</MinAttr1>
<MinAttr2>5</MinAttr2>
<Damage>100</Damage>
<AdditionalDmg>10</AdditionalDmg>
<AdditionalDmgType>Electric</AdditionalDmgType>
<Range>1</Range>
<Cost>1000</Cost>
</Weapon>
<Weapon id="1">
<Name>spada2</Name>
<MinLevel>1</MinLevel>
<MinAttr1>10</MinAttr1>
<MinAttr2>5</MinAttr2>
<Damage>100</Damage>
<AdditionalDmg>10</AdditionalDmg>
<AdditionalDmgType>Electric</AdditionalDmgType>
<Range>1</Range>
<Cost>1000</Cost>
</Weapon>
</Weapons>
This is Importer class that need the linq query for parsing:
Importer.cs
class Importer
{
public void ImportXML()
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
System.IO.StreamReader sr = new System.IO.StreamReader(openFileDialog1.FileName);
XDocument xml = XDocument.Load(sr);
sr.Close();
//Miss here linq query
}
}
}
}
My try (but "data" variable still null):
var data = from item in xml.Elements("weapon")
select new
{
name = item.Element("Name").Value,
minLevel = item.Element("MinLevel").Value
};
foreach (var p in data)
Debug.WriteLine(p.ToString());
Four issues:
The <Weapon> elements are children of the <Weapons> root element not the overall document.
XML is case sensitive, so when searching for elements or attributes by name you need to match the case exactly. Thus xml.Root.Elements("weapon") should be xml.Root.Elements("Weapon").
When converting an XElement value to a different type, say int, you should use explicit casting. This handles internationalization correctly. And do the same for XAttribute conversions.
"id" is an attribute, so use XElement.Attribute() to access it.
Thus:
var data = from item in xml.Root.Elements("Weapon")
select new
{
name = (string)item.Element("Name"),
minLevel = (int)item.Element("MinLevel"),
id = (int)item.Attribute("id")
};
The Elements children returns only direct children of a XContainer, so in your case xml.Elements("weapon") return the empty collection because there isn't any weapon element at the root of XML.
You should use
var data = from item in xml.Root.Elements("Weapon")
select new {
name = item.Element("Name").Value,
minLevel = item.Element("MinLevel").Value
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With