I have this type of XML file storing configuration values:
<assets>
<asset enabled="false" name="ListTypes" internalName="List" duplicateCheckField="" enableCustomFields="true"></asset>
<asset enabled="false" name="Members" internalName="Member" duplicateCheckField="Username" enableCustomFields="false"></asset>
</assets>
I would like to iterate through each asset and get each elements value, and store it in a list of assets.
I have the following code:
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
processConfiguration();
}
public static void processConfiguration()
{
XDocument assets = XDocument.Load("assets.xml");
List<asset> assetList = new List<asset>();
// this line is not working out for me as I cannot get each attr value
// by attr I mean enabled, name, etc for each row
foreach (var row in assets.Descendants("asset").ToList())
{
asset a = new asset();
// once i have the foreach correct I can build my list.
}
}
public class asset
{
public bool enabled { get; set; }
public string name { get; set; }
public string internalName { get; set; }
public string duplicateCheckField { get; set; }
public bool enableCustomFields { get; set; }
}
}
}
What am I missing here?
ToListin aforeach- there is no purpose. You can access the attributes using theXElement.Attribute()method:row.Attribute("name").Value.XAttributeimplements explicit casting so you can do stuff likea.enabled = (bool)row.Attribute("enabled");