首页 > c# > 将XML读入List <>

将XML读入List <> (Reading XML into List<>)

2017-06-22 c#xmllist

问题

第一次使用XML和许多实践,但是,我正在努力。我有一个简单的XML文件,一个Item类和一个List。我想读取XML文件并将其输出到Item,并将该Item添加到List中。

我已经弄清楚如何制作XML,读取它并模糊地将其解析为Item var但我无法包装或弄清楚如何转换它以将抓取的XML元素添加到我的List中。任何帮助都会感激不尽。

代码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>`

项目:

    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");
    }
}

方法:

    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());

}

解决方法

您可以将ref用于itemsList参数。

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())));

        }

使用时

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

我希望它会对你有所帮助。

问题

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());

}

解决方法

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.

相似信息