zoukankan      html  css  js  c++  java
  • 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);
                }

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

  • 相关阅读:
    tomcat使用不同的jdk版本 liunx 装两个jdk
    接下来自己的研究对象
    钉钉小程序开发的所有坑
    java 在web应用中获取本地目录和服务器上的目录不一致的问题
    Python2.7更新pip:UnicodeDecodeError: 'ascii' codec can't decode byte 0xb7 in position 7: ordinal not in range(128)
    vue项目中禁止移动端双击放大,双手拉大放大的方法
    JZ56 删除链表中重复的结点
    JZ55 链表中环的入口结点
    JZ54 字符流中第一个不重复的字符
    JZ53 表示数值的字符串
  • 原文地址:https://www.cnblogs.com/yangzhili/p/14772823.html
Copyright © 2011-2022 走看看