xml to class

Xml文件自动生成C# Class类 根据xml文件生成C# 属性class类,附件含源码。分享给需要的朋友 立即下载

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace XmlToClass
{
    public class XmlToClass
    {
        #region const
        private const string _type = "#type#";
        private const string _field = "#field#";
        private const string _property = "#property#";
        private const string _namespace = "#namespace#";
        private const string _class = "#class#";

        private const string property = @"
            private #type# _#field#;
            public #type# #property#
            {
                get{ return _#field#; }
                set{ _#field# = value; }
            }
            ";

        private const string filetextstart = @"
using System;
using System.Text;
using System.Collections.Generic;
namespace #namespace#.Model
{
    public class #class#
    {";

        private const string filetextend = @"
    }
}";
        #endregion

        #region Properties
        public Dictionary<string, string> Classes = new Dictionary<string, string>();
        public string Namespace { get; set; }
        public string SavePath { get; set; }
        #endregion

        public object ToClass(string xml)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

            foreach (XmlNode xn in doc.ChildNodes)
            {
                this.ToProperty(xn);
            }

            return null;
        }


        //如果有子节点(排除#text)
            //创建类
            //如果子节点多个且子节点相同
                //List<T>,类型名为子节点名
                //属性 = 进入第一个子节点
                //属性添加到类
            //否则
                //类型名为本节点名
                //循环所有子节点
                //{
                    //属性 = 进入子节点
                    //属性添加到类
                //}
                //保存
        //否则创建普通属性

        public string ToProperty(XmlNode node)
        {
            string type = string.Empty;
            string result = string.Empty;
            bool isList = false;

            if (node != null)
            {
                if (node.Attributes != null && node.Attributes.Count > 0 || (node.HasChildNodes && node.FirstChild.Name != "#text"))
                {
                    string start = XmlToClass.filetextstart.Replace(XmlToClass._namespace, this.Namespace);
                    StringBuilder classfile = new StringBuilder();

                    if (node.ChildNodes.Count > 1 && node.ChildNodes[0].Name == node.ChildNodes[1].Name)
                    {
                        isList = true;
                        type = node.ChildNodes[0].Name;
                        start = start.Replace(XmlToClass._class, type);
                        classfile.Append(start);
                        string str = this.ToProperty(node.ChildNodes[0]);
                        classfile.Append(str);
                        classfile.Append(XmlToClass.filetextend);
                    }
                    else
                    {
                        type = node.Name;
                        start = start.Replace(XmlToClass._class, type);
                        classfile.Append(start);
                        //xmlnode
                        foreach (XmlNode xn in node.ChildNodes)
                        {
                            string str = this.ToProperty(xn);
                            classfile.Append(str);
                        }
                        //XmlAttribute
                        foreach (XmlAttribute xa in node.Attributes)
                        {
                            string str = this.ToProperty(xa);
                            classfile.Append(str);
                        }
                        classfile.Append(XmlToClass.filetextend);
                        //保存
                        this.Classes.Add(type, classfile.ToString());
                    }
                }
                else
                {
                    type = this.GetValueType(node.InnerText);
                }
                if (isList)
                    type = "List<" + type + ">";
                result = this.ToProperty(node.Name, type);
            }

            return result;
        }

        public string ToProperty(XmlAttribute xa)
        {
             string type = string.Empty;
             type = this.GetValueType(xa.InnerText);
            return this.ToProperty(xa.Name, type);
        }

        public string GetValueType(string innerText)
        {
            string type = string.Empty;
            #region getType
            if (string.IsNullOrWhiteSpace(innerText))
                type = "string";
            else if (innerText.ToLower() == "true" || innerText.ToLower() == "false")
            {
                //bool
                type = "bool";
            }
            else
            {
                try
                {
                    try
                    {
                        try
                        {
                            //int32
                            Convert.ToInt32(innerText);
                            type = "int";
                        }
                        catch
                        {
                            //long
                            Convert.ToInt64(innerText);
                            type = "long";
                        }
                    }
                    catch
                    {
                        //double
                        Convert.ToDouble(innerText);
                        type = "double";
                    }
                }
                catch
                {
                    //string
                    type = "string";
                }
            }
            #endregion
            return type;
        }

        public string ToProperty(string name, string type)
        {
            string result = string.Empty;
            result = XmlToClass.property.Replace(XmlToClass._type, type);
            result = result.Replace(XmlToClass._field, name.ToLower());
            result = result.Replace(XmlToClass._property, name);
            return result;
        }

        public void SaveToProject()
        {
            string model = "Model";
            string extension = ".cs";
            if (!string.IsNullOrWhiteSpace(this.SavePath))
            {
                string direc = Path.Combine(this.SavePath, model);
                if (!Directory.Exists(direc))
                    Directory.CreateDirectory(direc);
                foreach (string key in this.Classes.Keys)
                {
                    using (FileStream fs = new FileStream(Path.Combine(direc, key + extension), FileMode.Create))
                    {
                        using (StreamWriter sw = new StreamWriter(fs))
                        {
                            sw.WriteLine(this.Classes[key]);
                        }
                    }
                }
            }
        }
    }
}

xmlto-0.0.25-7.el7.x64-86.rpm.tar.gz 1、文件内容:xmlto-0.0.25-7.el7.rpm以及相关依赖 2、文件形式:tar.gz压缩包 3、安装指令: #Step1、解压 tar -zxvf /mnt/data/output/xmlto-0.0.25-7.el7.tar.gz #Step2、进入解压后的目录,执行安装 sudo rpm -ivh *.rpm 4、更多资源/技术支持:公众号禅静编程坊 立即下载

相关推荐

Calibre提参实战:如何用hcell和xcell文件精准屏蔽模块(附避坑指南)

本文详细解析了Calibre提参过程中hcell和xcell文件的应用技巧,帮助工程师精准屏蔽模块以优化寄生参数提取。通过对比hcell与xcell的核心差异、图形界面与命令行操作指南,以及常见问题排查方法,提升芯片后端设计效率。附避坑指南,解决EETOP论坛热议的模块丢失问题。

weixin_26795617的博客 438

BQ40Z50R2循环学习实战:量产文件生成与关键状态位解析

本文详细解析了TI BQ40Z50R2电量计循环学习(Golden Learning)的实战流程与量产文件生成方法。重点剖析了updateStatus状态位在充放电静置阶段的常见异常(如卡在0x04或跳至0x0E)及其根本原因,并提供了从环境控制、命令顺序到文件处理的完整避坑指南,帮助工程师高效完成量产配置。

weixin_29170327的博客 214

C# 序列化xml数据到class

public static class XmlClassData     {         /// 从XML读取数据         /// 读取的数据类型         /// 包含数据的文件 FileInfo 信息         /// 返回为 null 的时候读取失败         public static C ReadDataFromXml(t

CRUD程序员的博客 812

CST实战:5分钟搞定圆极化天线轴比分析(含远场设置技巧)

本文提供了一份CST Studio Suite圆极化天线轴比分析的实战指南。文章详细讲解了轴比的概念与工程标准,并重点演示了如何正确设置远场监视器以获取Axial Ratio数据,包括关键步骤与后处理技巧。最后,通过植入式天线在生物组织环境中的案例,分析了复杂环境对轴比性能的影响及优化思路。

nokia的博客 996

在WinXP操作系统上安装Tornado2.2详细教程

使用VxWorks的人都想在自己的电脑中装VxWorks的编译器Tornado,然而Win10操作系统对于Tornado并不友好,根本装不成功。而对于Win7操作系统,之前在实验室一台Win7上装Tornado成功过,并且一直在上面进行vx开发,但是过一段时间在其他Win7上装Tornado时又不能成功,由此说明Tornado对于Win7操作系统对Tornado的兼容也不是很好,所以想要长久使用T...

weixin_43822311的博客 4481

xml生成java类在线

XML to Java Class Generator是一个非常有用的在线工具,它可以帮助我们快速生成Java类代码来处理XML数据。通过简单的复制粘贴,我们可以获得可读性和维护性良好的Java类,从而提高开发效率。这个工具在处理小到中等规模的XML文件时非常适用,并且提供了一些自定义选项来满足不同的需求。它解决了手动编写Java类以解析XML的繁琐问题,让我们能够更专注于业务逻辑的实现。

牛肉胡辣汤 2848

ZYNQ-Video out IP与Video Timing Controller IP协同工作模式解析

本文深入解析了ZYNQ平台上Video out IP与Video Timing Controller IP的协同工作模式,详细介绍了它们的架构、工作原理及配置要点。通过AXI4-Stream接口实现高效视频数据传输,并结合HDMI输出系统设计实践,为开发者提供了实用的调试技巧和性能优化建议。

redis7keeper的博客 637

c# xml和类互转,支持泛型

语言:C# 开发环境:VS2012 功能:xml格式字符串与类互转 代码: using System.Xml.Serialization; using System.IO; namespace wjl.common { /// /// xml操作转换类 /// public class XmlParse { ///

战争迷雾的专栏 2301

工业4.0 底层逻辑

工业4.0 的概念已经被滥用了,它与物联网,工业物联网等概念一样,几乎什么都往里面装。演变成了一句口号和愿景。许多人并不清楚工业4.0的底层逻辑到底是什么?如何遵循工业4.0 的思想构建新一代智能制造系统?并没用清晰地展示工业4.0的技术路线图。

姚家湾博客 1993

Python计算时间差的方法

使用datetime模块是Python内置的方法,使用timedelta对象可以更精确地计算时间差,而使用dateutil库则提供了更为灵活和方便的操作方式。我们可以使用datetime模块中的datetime类来表示一个具体的日期和时间,并进行相应的运算。在Python中,我们经常需要计算日期和时间之间的差异,例如计算两个日期之间的天数、小时数、分钟数等。Python提供了多种方法来计算时间差,下面我们将介绍其中的几种常用方法,并附上相应的源代码。在上面的代码中,我们使用了dateutil库中的。

学习使你进步。 2086

error:Unable to load class ‘javax.xml.bind.JAXBException‘.

本文参考博客 错误: 解决: 在 app的 build中添加 javaCompileOptions{ ... } android { … defaultConfig { … //添加如下配置 javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } } } … } 注:出现此问题的原因有几种,同一个异常,不一定是同一个解决方案,可参考一下博客 1

你的博客 2万+

实现边缘实时动物检测:YOLOv5模型优化与树莓派4B部署全攻略

本文介绍了基于高性能计算机训练YOLOv5目标检测模型,并部署到树莓派4B实现实时动物检测的全流程。主要内容包括:1)在高性能计算机上通过Anaconda创建虚拟环境,安装YOLOv5项目并进行模型训练;2)将训练好的best.pt模型迁移到树莓派,配置轻量级Miniconda环境;3)连接树莓派摄像头,运行实时检测程序。该系统兼顾了训练效率和边缘设备部署的实用性,可广泛应用于智慧农业、生态监测等场景。文中详细说明了数据准备、环境配置和部署步骤,为开发轻量级目标检测系统提供了完整参考方案。

m0_64661770的博客 201

Unable to load class ‘javax.xml.bind.JAXBException‘.

android studio 4.2时出现, Unable to load class 'javax.xml.bind.JAXBException'. This is an unexpected error. Please file a bug containing the idea.log file. 谷歌查看,好像与JDK版本有关,要用JDK8才行,下载JDK8后,发现项目无法更改adk路径,file->project structure->SDK Location->JDK

m0_37910557的博客 9988

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

Unable to load class ‘javax.xml.bind.annotation.XmlSchema‘.

Unable to load class ‘javax.xml.bind.annotation.XmlSchema’. This is an unexpected error. Please file a bug containing the idea.log file. 在新版本的Android Studio上打开老项目出现这个问题。 修改了JDK路径之后点击OK或者Apply都可以,基本上就没有问题了,出现这个问题的原因是AS默认让你使用了JDK11,改回之前的JDK8就可以了。 如果后面然后又出现这

晨曦 3万+

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

unity class to xml

编写:solqblog:http://www.cnblogs.com/solq 资源放在Assets\StreamingAssets\ 目录 SXMLTool.cs /*编写:solq blog:www.cnblogs.com/solq */ using UnityEngine; using System.Xml.Serialization; using System.IO;...

weixin_30500663的博客 129

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

Unable to load class ‘javax.xml.bind.JAXBException‘. This is an unexpected error

AndroidStudio报错:Unable to load class 'javax.xml.bind.JAXBException'. This is an unexpected error 原因:升级androidstudio4.2.1后,默认jdk的问题 解决方案修改为自己安装的jdk即可。

景兄弟的博客 6954

Python 之metaclass应用举例 - xml to class

虽然从我开始接触python语言至今不到一年时间,对python语言的理解还比较浅薄,但是python语言本身之灵活给我带来了一次次惊喜,下面给大家介绍下使用python的metaclass(元类)以及自定义import行为来实现通过xml批量定义class的方法。 metaclass可以理解为类的构造器,具体python是如何实现如此神奇的功能我还没有深入学习,但是metaclass使用起来确

kingofring的专栏 519

Android出现Unable to load class ‘javax.xml.bind.annotation.XmlSchema‘.的解决方法

Android出现Unable to load class 'javax.xml.bind.annotation.XmlSchema'.的解决方法

weixin_43456810的博客 6567

How to export class from xml schema file

1. Define a schema file: **.xsd 2. create class for this schema file 2.1 find the location of schema file 2.2 start "Visual Studio Command Prompt" under VS2010 tools 2.3 switch current location to

marryshi的专栏 646

How to parse XML file using CParser class

Reviewer Approved     The following example shows how to parse XML file using Symbian OS C++ class, CParser. CParser is basically a SAX (Simple API for XML)-based XML parser. It uses an active object

World of iOs 804

更新AS导致更新gradle导致的问题:Gradle sync failed: Unable to load class 'javax.xml.bind.annotation.XmlSchema

最近AndroidStudio更新后,发现无法完成gradle sync,报的错误忘记截图了,反正上网查了说是gradle版本和JDK不兼容,就是我的gradle太低了,需要更新(这真是很奇怪,因为AS没有更新又不更新JDK的,怎么以前没有问题?萌新不懂)于是我就上网搜相关的博客更新,步骤也很简单, 其中最后一步是: 在android目录下,找到 build.gradle(注意有两个,选这个:bu...

weixin_43890557的博客 2万+

A simple class for converting any Java object to XML string

A simple class for converting any Java object to XML string In need to save XML representation of your Java object? Here is a simple 200-line class that will do this using reflection. But dont worry,

1135

JAXB转XML: 错误 class nor any of its super class is known to this 的处理

@XmlElement(name="")  type 类型没有指定

道然的博客 4828

Unable to load class 'javax.xml.bind.JAXBException'

实际的报错: Unable to load class 'javax.xml.bind.JAXBException'. This is an unexpected error. Please file a bug containing the idea.log file. 产生的原因: 我也没有去做太多的分析 ,基本上是 gre和jdk和项目兼容的问题或 你的项目找不到你的本地java 环境 解决...

Hu_wenpeng的博客 585

Unable to load class ‘javax.xml.bind.JAXBException‘

distributionUrl=https://services.gradle.org/distributions/gradle-6.7.1-bin.zip --此方法解决。1.尝试把jdk编译版本改成1.8;但我的AS版本最低要求要11. —失败。AS导入一个旧项目,报这个错。看了很多方法都不能解决我的问题。3.提高gradle版本;bulid.gradle中修改为。gradle-wrapper.properties中修改为。

qq_30372521的博客 521
上一篇: ListView用法
下一篇: 异步与同步通信
bemodesty
博客等级 码龄17年 22粉丝 1原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值