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

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

Linq操作XML生成XML,实体生成XML和互转的更多相关文章

  1. 使用mybatis-generator-core工具自动生成mybatis实体

    我们可以使用mybatis-generator-core这个工具将数据库对象转换成mybatis对象,具体步骤如下. 1.mybatis-generator-core下载 下载地址:http://do ...

  2. 生成XML文件,通过实体生成XML文件

    实体 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xm ...

  3. Mybatis Generator生成Mybatis Dao接口层*Mapper.xml以及对应实体类

    [前言] 使用Mybatis-Generator自动生成Dao.Model.Mapping相关文件,Mybatis-Generator的作用就是充当了一个代码生成器的角色,使用代码生成器不仅可以简化我 ...

  4. C# Linq方式生成SAP对接的XML格式内容(一般处理程序 ashx )

    Linq生成XML的方法: string CreateXML(string strkey, string strDATAJSON) { XDeclaration dec = new XDeclarat ...

  5. Mybatis的逆向工程,自动生成代码(Mapper,xml,bean)

    步骤: 1. 新建一个Maven项目: 然后导入maven依赖: <dependencies> <dependency> <groupId>org.mybatis& ...

  6. 生成当前目录文件的xml描述

    需求场景:例如需要在当前目录下把相关文件组织成xml文件去描述.通常在组织项目中的升级文件时候可能会用到. 代码示例: using System; using System.Collections.G ...

  7. Android中XML文件的序列化生成与解析

    xml文件是非常常用的,在android中json和xml是非常常用的两种封装数据的形式,从服务器中获取数据也经常是这两种形式的,所以学会生成和解析xml和json是非常有用的,json相对来说是比较 ...

  8. Android学习记录(1)—Android中XML文件的序列化生成与解析

    xml文件是非常常用的,在android中json和xml是非常常用的两种封装数据的形式,从服务器中获取数据也经常是这两种形式的,所以学会生成和解析xml和json是非常有用的,json相对来说是比较 ...

  9. 5.20 mybatis反向生成的映射文件xml(如果需要自己定义其他sql语句时如下)

    解决mybatis-generator 生成的mapper.xml覆盖自定义sql的问题 mybatis-generator是个好工具,一建即可生成基本增删改成功能的mapper.xml.但这些是不够 ...

  10. Android 生成和Pull解析xml

    一.单个对象生成xml 生成以下xml,该怎么生成呢? <?xml version='1.0' encoding='UTF-8' standalone='yes' ?> <accou ...

随机推荐

  1. [BJDCTF2020]Mark loves cat(源码泄露+命令执行)

    扫描之后发现是/.git源码泄露 python GitHack.py http://56ad87c1-d8fb-463d-9480-f0fbee5176a0.node5.buuoj.cn:81/.gi ...

  2. 用ssh同时push 项目到github和gitee的方法

    ​ 分别为两个网站声称pubkey cd ./ssh ssh-keygen -t rsa -C "oeasy@oeasy.org" -f "github_id_rsa&q ...

  3. 【VMware VCF】VMware Cloud Foundation Part 01:概述。

    VMware Cloud Foundation(简称 VCF)是 VMware 打造的一套用于 Software Defined Data Center(SDDC)软件定义数据中心的全栈云平台解决方案 ...

  4. C# 常用类和命名空间

    Array类 用括号声明数组是C#中使用Array类的记号.在后台使用C#语法,会创建一个派生于抽象基类Array的新类.这样,就可以使用Array类为每个C#数组定义的方法和属性了. Array类实 ...

  5. 彻底搞懂python super函数的作用

    super() 的入门使用 在类的继承中,如果重定义某个方法,该方法会覆盖父类的同名方法,但有时,我们希望能同时实现父类的功能,这时,我们就需要调用父类的方法了. 调用父类同名方法有两种方式: 1.调 ...

  6. 支付宝小程序的级联选择器,对接简单操作,Cascader 级联选择器element_ui

    首先,对于element_ui 的动接,由于需要数据格式是 但是支付宝提的接口返回的数据是另一种格式,并且支付宝的三级联动接口是先只有一个列表,点击列表项再发现请求,生成另外一个下拉选择, 需要这个三 ...

  7. 如何正确使用@Bulider与<T>返回数据

    @Data @ToString @Builder @AllArgsConstructor public class PageResult<T> implements Serializabl ...

  8. 【ElasticSearch】03 部署

    Windows集群部署: 把包解压成三个节点 [kibana-7.16.3-windows-x86_64.zip] - Node-1001 - Node-1002 - Node-1003 修改集群的配 ...

  9. 【Shiro】07 散列算法 & 凭证配置

    [散列算法概述] 用于生成数据的摘要信息,不可逆算法,用于存储密码或者密文数据. 常见散列算法类型:MD5.SHA 一般进行散列时提供一个"盐",即系统知道的"干扰数据& ...

  10. 【Layui】09 动画 Anim

    文档地址: https://www.layui.com/demo/anim.html 8种动画 <fieldset class="layui-elem-field layui-fiel ...