Reputation: 155
I am very new to C# but attempting to make an app to parse a simple XML file like shown below and populate the data inside a multi-column ListView.
<mods>
<mod Name="mod1.zip" sha1="508ed8f5fcd7d323d9296acad76f1183b810f62a"/>
<mod Name="mod2.zip" sha1="669d8d09d297a9724fe1d1d676ac5f5a8ff10277"/>
</mods>
Here is the code I'm trying to work from but it isn't populating the ListView like I'd expect it to..
modList.View = View.Details;
modList.Columns.Add("Name", 650);
modList.Columns.Add("Status", 111, HorizontalAlignment.Center);
XElement root = XElement.Load("https://website.com/modlist.xml");
var mods = from subject in root.Descendants()
where subject.Name.LocalName.Contains("Mod")
select new
{
Name = subject.Element("Name").Value,
Hash = subject.Element("Hash").Value,
};
foreach (var mod in mods)
{
modList.Items.Add(new ListViewItem(new string[]
{
mod.Name,
mod.Hash
}));
}
Upvotes: 0
Views: 100
Reputation: 2057
You can get all mods using Descendants("mod")
. And the attribute using Attribute("Name")
var mods = from ele in xDoc.Descendants("mod")
select new
{
Name = (string)ele.Attribute("Name"),
Hash = (string)ele.Attribute("sha1")
};
You can use Attributes()
to list all the attributes.
If you have to handles Hash beeing in a SHA-1, SHA-2 , or SHA-256 attribute
Upvotes: 2