开发接口中难免会遇到一些数据对接需要转换城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. @ConfigurationProperties 还能这样用

    在编写项目代码时,我们要求更灵活的配置,更好的模块化整合.在 Spring Boot 项目中,为满足以上要求,我们将大量的参数配置在 application.properties 或 applicat ...

  2. 洛谷·P1130

    #include<iostream> #include<utility> using namespace std; typedef long long ll; #define ...

  3. ES6拼接数组与小程序本地存储

    拼接数组 ES6扩展运算符[三个点(...)将一个数组转为用逗号分隔的参数序列] goodsList: [...goodsList, ...goods] 本地存储 // 把接口数据存入本地存储中 wx ...

  4. Solo 独立开发者社区

    Solo 独立开发者社区是一个致力于帮助独立开发者发展的社区平台.平台上聚集了大量的独立开发者,他们在这个平台上分享着自己的开发经验.心得和技巧,同时也可以从其他人的分享中获得启发和帮助. 这个社区提 ...

  5. 解决php提示Maximum execution time of 30 seconds exceeded错误

    如何解决错误? 基本上,有3种方法可以处理此错误: 修改php配置文件php.ini文件 使用 ini_set() 函数 使用set_time_limit()函数 1)修改php配置文件php.ini ...

  6. [oeasy]python0137_相加运算_python之禅_import_this_显式转化

    变量类型 相加运算 回忆上次内容 上次讲了是从键盘输入变量 input 函数 可以有提示字符串 需要有具体的变量接收输入的字符串   输入单个变量没有问题 但是输入两个变量之后 一相加就非常离谱   ...

  7. 新做了一个MySQL 数据库 DDL 差异对比的网站

    MySQL 数据库 DDL 差异对比的网站 摘要 新做了个网站,用来对比不同环境下的 DDL 差异,生成变更点和 迁移 DDL 网站地址:https://ddlcompare.com/ 对比过程中如果 ...

  8. 标准 C++ 中的 string 类的用法总结

    相信使用过 MFC 编程的朋友对 CString 这个类的印象应该非常深刻吧?的确,MFC 中的 CString 类使用起来真的非常的方便好用.但是如果离开了 MFC 框架,还有没有这样使用起来非常方 ...

  9. 使用nvm管理node

    安装包在阿里云盘 安装时,记录安装位置 安装后,在安装目录的setting添加镜像地址 node_mirror:npm.taobao.org/mirrors/node/ npm_mirror:npm. ...

  10. 《最新出炉》系列入门篇-Python+Playwright自动化测试-55- 上传文件 (非input控件)- 中篇

    1.简介 在实际工作中,我们进行web自动化的时候,文件上传是很常见的操作,例如上传用户头像,上传身份证信息等.所以宏哥打算按上传文件的分类对其进行一下讲解和分享. 2.上传文件的API(非input ...