ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-05-15 23:04:03  阅读:214  来源: 互联网

标签:XML xml node XElement 生成 互转 new id


开发接口中难免会遇到一些数据对接需要转换城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);
            }

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

标签:XML,xml,node,XElement,生成,互转,new,id
来源: https://www.cnblogs.com/yangzhili/p/14772823.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有