I'm trying to load an XML file into a ListView, however, every time I select the file, I get an error which says
System.Xml.Linq.XContainer.Element(…) returned null
This is my code:
XDocument doc = XDocument.Load(typesXML);
IEnumerable<XElement> serverElements = doc.Descendants("type");
foreach (XElement serverElement in serverElements)
{
ListViewItem item = new ListViewItem(new string[]
{
serverElement.Element("nominal").Value,
serverElement.Element("lifetime").Value,
serverElement.Element("restock").Value,
serverElement.Element("min").Value,
serverElement.Element("quantmin").Value,
serverElement.Element("quantmax").Value,
serverElement.Element("cost").Value
});
listView1.Items.Add(item);
}
XML:
<types>
<type name="ACOGOptic">
<nominal>15</nominal>
<lifetime>7200</lifetime>
<restock>1800</restock>
<min>8</min>
<quantmin>-1</quantmin>
<quantmax>-1</quantmax>
<cost>100</cost>
<flags count_in_cargo="0" count_in_hoarder="0" count_in_map="1" count_in_player="0" crafted="0" deloot="0"/>
<category name="weapons"/>
<usage name="Military"/>
</type>
</types>
Also, how would I go when I also want the tag "name" from "type" load into my ListView?
It worked with using DataSet, but I want to use it like this.