0

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?

3
  • 3
    Don't use ToList in a foreach - there is no purpose. You can access the attributes using the XElement.Attribute() method: row.Attribute("name").Value. Commented Oct 3, 2018 at 18:51
  • 1
    Also note that XAttribute implements explicit casting so you can do stuff like a.enabled = (bool)row.Attribute("enabled"); Commented Oct 3, 2018 at 18:53
  • @NetMage Add that as the answer so you get credit for saving some time for me. Commented Oct 3, 2018 at 19:00

1 Answer 1

1

You can use XElement.Attribute to access the attribute values. You can also use LINQ to XML to shorten the code:

XDocument assets = XDocument.Load("assets.xml");
var assetList = assets.Descendants("asset")
                      .Select(row => new asset {
                           enabled = (bool)row.Attribute("enabled"),
                           name = row.Attribute("name").Value,
                           internalName = row.Attribute("internalName").Value,
                           duplicateCheckField = row.Attribute("duplicateCheckField").Value,
                           enableCustomFields = (bool)row.Attribute("enabledCustomFields")
                       }).ToList();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.