Reading value of an XML node

前端 未结 3 921
执笔经年
执笔经年 2021-01-28 22:54

i need to get the value of an Node in an XML File.

My XML file looks like this:





        
3条回答
  •  无人共我
    2021-01-28 23:20

    You can use Linq To Xml.

    Assuming you you have other products like IPHONE under PRODUCTS

    var products = XDocument.Load(filename).Root
                    .Elements()
                    .Select(x => new
                    {
                        Product = x.Name.LocalName,
                        Name = (string)x.Element("NAME"),
                        Model = (string)x.Element("MODEL"),
                        Price = (decimal)x.Element("PRICE"),
                        Color = (string)x.Element("COLOR")
                    })
                    .ToList();
    

提交回复
热议问题