Linq操作XML生成XML,实体生成XML和互转

开发接口中难免会遇到一些数据对接需要转换城xml,看到很多之前的代码都使用很传统的方法循环集合并定义xml后一一生成的,代码之封锁 特此使用了简单易用linq操作分享给大家,希望可以帮到需要的同学

今天就带大家简单使用Linq生成XML,及Linq操作XML的基本操作。实体生成XML和XML生成实体互转

一、实体→XM的操作

如以下是演示的产品实体模型如下:

    public class Product
    {
        public int id { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }
        public int Category { get; set; }
    }

操作集合list,生成xml

            //构造一个商品集合

            List<Product> list = new List<Product>();
            for (int i = 1; i < 21; i++)
            {
                list.Add(new Product { id = 1, Name = $"商品{i}", Price = new Random().Next(i, 1000), Category = new Random(i).Next(20) });
            }
            var xml = new XElement("products",
                from p in list
                select new XElement("product",
                new XAttribute("id", p.id),
                new XAttribute("name", p.Name),
                new XElement("price", p.Price),
                new XElement("category", p.Category)));

            Console.WriteLine(xml.ToString());

 那么问题来了,当我们接口接收到xml时候如何又将xml转换为实体尼?

一、XML→实体的操作

操作是一样的快捷简单,linq肯定是少不了的,item.xml是文件,此处也可以是读取文件流,大家可以根据自己的需求修改。

            XDocument document = XDocument.Load("item.xml");
            var collection = document.Descendants("products")
                .Descendants("product").Select(
                node => new Product
                {
                    id = Convert.ToInt16(node.Attribute("id")),
                    Name = node.Attribute("name").Value,
                    Price = Convert.ToDecimal(node.Attribute("rice")),
                    Category = Convert.ToInt16(node.Attribute("category"))
                });

最终完整代码:

            List<Product> list = new List<Product>();
            for (int i = 1; i < 21; i++)
            {
                list.Add(new Product { id = 1, Name = $"商品{i}", Price = new Random().Next(i, 1000), Category = new Random(i).Next(20) });
            }
            var xml = new XElement("products",
                from p in list
                select new XElement("product",
                new XAttribute("id", p.id),
                new XAttribute("name", p.Name),
                new XElement("price", p.Price),
                new XElement("category", p.Category)));

            Console.WriteLine(xml.ToString());


            XDocument document = XDocument.Load("item.xml");
            var collection = document.Descendants("products")
                .Descendants("product").Select(
                node => new Product
                {
                    id = Convert.ToInt16(node.Attribute("id")),
                    Name = node.Attribute("name").Value,
                    Price = Convert.ToDecimal(node.Attribute("rice")),
                    Category = Convert.ToInt16(node.Attribute("category"))
                });

            foreach (var item in collection)
            {
                Console.WriteLine(item.Name);
            }

简单快捷明了,不知是否帮到了正在看帖的你?

原文地址:https://www.cnblogs.com/yangzhili/p/14772823.html