Chad E
Chad E

Reputation: 37

Reading XML into List<>

first time working with XML and a lot of the practices in this but, I'm working through it. I have a simple XML file, an Item class and a List. I want to read the XML File and output it to and Item, and add that Item to the List.

I've figured out how to make the XML, read it and vaguely parse it to a Item var but I can't wrap or figure out how to convert this to add the grabbed XML element to my List. any help would be grateful.

Code XML:`

<?xml version="1.0" encoding="utf-8" ?>
<ITEMS>
  <ITEM>
    <ITEMNAME>Apple</ITEMNAME>
    <ITEMPRICE>2.50</ITEMPRICE>
  </ITEM>
  <ITEM>
    <ITEMNAME>Dark Chocolate</ITEMNAME>
    <ITEMPRICE>1.25</ITEMPRICE>
  </ITEM>
</ITEMS>`

Item:

    internal class Item
{
    private string _itemName;
    private decimal _itemPrice;

    public Item(string itemName, decimal itemPrice)
    {
        _itemName = itemName;
        _itemPrice = itemPrice;
    }

    public Item()
    {
    }

    public string ItemName
    {
        get => _itemName;
        set => _itemName = value;
    }

    public decimal ItemPrice
    {
       get { return _itemPrice;}
        set { _itemPrice = value; }
    }

    public override string ToString()
    {
        // String representation.
        return this._itemName + " " + this._itemPrice.ToString("C");
    }
}

Method:

    public static void CreateItemsList(List<Item> itemsList)
{
    var doc = XDocument.Load(@"..\..\ItemsXML.xml");

    var item = doc.Root
        .Descendants("ITEM")
        .Select(node => new Item
        {
            ItemName = node.Element("ITEMNAME").Value,
            ItemPrice = decimal.Parse(node.Element("ITEMPRICE").Value)
        })
        .ToList();
    Console.WriteLine(item.ToString());

}

Upvotes: 1

Views: 4657

Answers (3)

dovid
dovid

Reputation: 6491

foreach (var oneItem in item)
    Console.WriteLine(oneItem.ToString());

var item = ...(node => new item ... ).ToList() this code place in Item a List<item>. Therefore, to call ToString of each individual, you have to go through the list.

For this reason, consider changing his name to "items".

Upvotes: 0

Tien Nguyen Ngoc
Tien Nguyen Ngoc

Reputation: 1555

You can use ref for itemsList parameter.

public static void CreateItemsList(ref List<Item> itemsList)
        {
            var doc = XDocument.Load(@"..\..\ItemsXML.xml");

            itemsList = doc.Root
                .Descendants("ITEM")
                .Select(node => new Item
                {
                    ItemName = node.Element("ITEMNAME").Value,
                    ItemPrice = decimal.Parse(node.Element("ITEMPRICE").Value)
                })
                .ToList();

            Console.WriteLine(string.Join(",", itemsList.Select(x => x.ToString())));

        }

When use

    List<Item> itemsList = new List<Item>();
    CreateItemsList(ref itemsList);

I hope it will help you.

Upvotes: 3

jdweng
jdweng

Reputation: 34433

The code was working, just the ToString() wasn't. Made some minor improvements :

       public static void CreateItemsList(List<Item> itemsList)
        {
            var doc = XDocument.Load(FILENAME);

            var item = doc.Root
                .Descendants("ITEM")
                .Select(node => new Item()
                {
                    ItemName = (string)node.Element("ITEMNAME"),
                    ItemPrice = (decimal)node.Element("ITEMPRICE")
                })
                .ToList();
            Console.WriteLine(string.Join(",",item.Select(x => x.ToString())));

        }

Upvotes: 0

Related Questions