由xml生成xsd及实体类

 

使用VS2005工具XSD.exe(SDK/v2.0/Bin/xsd.exe)自动生成实体类:

xsd /c /namespace:myCompany /language:CS temp1.xsd

也可以生成DataSet类型的类:

xsd /dataset /language:CS temp1.xsd

( 类文件和XSD之间可以相互转换,也就是说,你也可以先生成类,然后自动生成XSD)

自动读取XML数据到实体类:

XmlSerializer xs = new XmlSerializer(typeof(myClassType)); using (FileStream fs = new FileStream(XmlFilePath, FileMode.Open)) {     return (myClassType)xs.Deserialize(fs); }

use XSD.exe in VS2010 from a xsd file to class

1.use XSD.exe

Start -> All Programs -> Microsoft Visual Studio 2010 -> Visual Studio Tools -> Visual Studio Command Prompt

2.from a xsd file to class

xsd /c /namespace:myCompany /language:CS temp1.xsd

系统:Windows 2003 Server SP2

工具:VS2008

第一步:创建Xml文件架构

首先在VS2008中打开编写好的Xml文件(或在VS2008中直接编辑)-->点击工具栏的“Xml”(如果打开的不是Xml文件,此菜单不显示)

-->点击“创建架构”(会生产一个*.xsd的文件)-->把此文件进行保存(记住保存的目录,之后要用到)-->完毕。

第二步:通过架构生成实体访问类

首先从“开始”-->“所有程序”-->“Microsoft Visual Studio 2008”-->“Visual Studio Tools”中打开“Visual Studio 2008命令提示”,然后在里面输入“xsd.exe /c /l:c# 刚才文件保存的路径”,执行以下就OK了,生成的实体访问类在执行界面有显示,一般为:“C:/Program Files/Microsoft Visual Studio 9.0/VC/*.cs”。

第三步:定义读取访问类

把生产的Xml拷贝到使用程序中,并增加如下访问类:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Serialization;
  6. namespace 命名空间
  7. {
  8. public class XmlInstanceClass
  9. {
  10. public static configuration GetInterFaceConfig()
  11. {
  12. configuration ResultCon = null;
  13. string XmlPath = 程序中的Xml路径;
  14. System.IO.FileStream FS = null;
  15. System.Xml.Serialization.XmlSerializer XmlSerializer = new XmlSerializer(typeof(configuration));
  16. try
  17. {
  18. FS = new System.IO.FileStream(XmlPath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
  19. ResultCon = (configuration)XmlSerializer.Deserialize(FS);
  20. }
  21. catch (Exception ex)
  22. {
  23. throw new Exception(ex.Message);
  24. }
  25. finally
  26. {
  27. FS.Close();
  28. }
  29. return ResultCon;
  30. }
  31. }
  32. }

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

namespace 命名空间
{
public class XmlInstanceClass
{
public static configuration GetInterFaceConfig()
{
configuration ResultCon = null;
string XmlPath = 程序中的Xml路径;
System.IO.FileStream FS = null;

System.Xml.Serialization.XmlSerializer XmlSerializer = new XmlSerializer(typeof(configuration));
try
{
FS = new System.IO.FileStream(XmlPath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
ResultCon = (configuration)XmlSerializer.Deserialize(FS);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
FS.Close();
}
return ResultCon;
}
}
}

第四步:在其他类中使用

configuration ResultCon=XmlInstanceClass.GetInterFaceConfig();

这样值就读入了ResultCon,之后就可以根据业务自定义使用了。

备注:如果对“xsd.exe ”命令不太熟悉,可以直接输入xsd.exe点击“Enter”查看帮助说明;

‘/c’:代表输出的为类文件;

‘/l:c#’:代表输出的类文件所使用的编程语言。

XML文件与实体类的互相转换

一.将XML文件反序列化为实体类对象

  1. 通常程序的配置信息都保存在程序或者网站的专门的配置文件中(App.config/web.config)。但是现在为了演示XML序列化和反序列化,将配置信息保存在一个XML文件(config.xml)中,通过反序列化将配置信息读取出来保存到一个单独的类(Config.cs)中。这样如果需要用到配置信息,没必要每次都读写XML文件,只需要调用Config这个类就可以获取对应节点的信息。

  config.xml:

<?xml version="1.0" encoding="utf-8"?>
<Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" IsAuto="true"> <Description>定时扫描数据库,通过客户号和业务号读取客户信息</Description> <CustomerInfos>
<CustomerInfo>
<CustomerId>0013</CustomerId>
<BusinessId>03</BusinessId>
</CustomerInfo>
<CustomerInfo>
<CustomerId>0022</CustomerId>
<BusinessId>02</BusinessId>
</CustomerInfo>
</CustomerInfos> <ScanConfigs>
<BeginTime>22:00:00</BeginTime>
<EndTimme>23:00:00</EndTimme>
</ScanConfigs> </Config>

  2. 为了将上面这个XML转换为想要的实体类对象,方便在程序里面读取节点数据,需要创建一个相对应的实体类,在实体类中用[XmlRoot][XmlElement][XmlAttribute]等属性标识。

  Config.cs:

  //XmlRoot表明这个类对应的是XML文件中的根节点
  [XmlRoot(ElementName="Config")]
public class Config
{
     //XmlElement表明这个字段对应的是XML文件中当前父节点下面的一个子节点
     //ElementName就是XML里面显示的当前节点名称
     //类中的字段名称与对应的XML节点的名称可以不同(比如在这里类Config中的属性ClientDescription对应XML文件中根节点Config下面的子节点Description)
[XmlElement(ElementName = "Description")]
public string ClientDescription { get; set; }      //XmlAttribute表明这个字段是XML文件中当前节点的一个属性
[XmlAttribute(AttributeName="IsAuto")]
public string IsAuto { get; set; } [XmlElement(ElementName = "CustomerInfos")]
public CustomerInfos CustomerInfos
{
get;
set;
} [XmlElement(ElementName = "ScanConfigs")]
public ScanConfigs ScanConfigs
{
get;
set;
}
} public class CustomerInfos
{
[XmlElement(ElementName = "CustomerInfo")]
public CustomerInfo[] cs
{
get;
set;
}
} public class CustomerInfo
{
[XmlElement(ElementName = "CustomerId")]
public string CustomerId { get; set; } [XmlElement(ElementName = "BusinessId")]
public string BusinessId { get; set; }
} public class ScanConfigs
{
[XmlElement(ElementName = "BeginTime")]
public string BeginTime { get; set; } [XmlElement(ElementName = "EndTimme")]
public string EndTimme { get; set; }
}

  3. 下面的代码调用.net的XmlSerializer类的方法进行XML的反序列化

  public class XmlUtil
{
//反序列化
     //接收2个参数:xmlFilePath(需要反序列化的XML文件的绝对路径),type(反序列化XML为哪种对象类型)
public static object DeserializeFromXml(string xmlFilePath, Type type)
{
object result = null;
if (File.Exists(xmlFilePath))
{
using (StreamReader reader = new StreamReader(xmlFilePath))
{
XmlSerializer xs = new XmlSerializer(type);
result = xs.Deserialize(reader);
}
}
return result;
}
}

  4. 反序列化

   string xmlPath = "d:\\config.xml";
Config c = XmlUtil.DeserializeFromXml(xmlPath, typeof(Config)) as Config;

二. 序列化

  1. 反过来的,也可以将Config类的一个对象序列化为XML文件.下面的代码通过调用.net的XmlSerializer类的方法将对象序列化为XML文件

  public class XmlUtil
{
//序列化
     //接收4个参数:srcObject(对象的实例),type(对象类型),xmlFilePath(序列化之后的xml文件的绝对路径),xmlRootName(xml文件中根节点名称)
     //当需要将多个对象实例序列化到同一个XML文件中的时候,xmlRootName就是所有对象共同的根节点名称,如果不指定,.net会默认给一个名称(ArrayOf+实体类名称)
public static void SerializeToXml(object srcObject, Type type,string xmlFilePath, string xmlRootName)
{
if (srcObject != null && !string.IsNullOrEmpty(xmlFilePath))
{
type = type != null ? type : srcObject.GetType(); using(StreamWriter sw=new StreamWriter(xmlFilePath))
{
XmlSerializer xs = string.IsNullOrEmpty(xmlRootName) ?
new XmlSerializer(type) :
new XmlSerializer(type, new XmlRootAttribute(xmlRootName));
xs.Serialize(sw, srcObject);
}
}
}
}

  2. 序列化

       Config config = new Config();
config.ClientDescribe = "定时扫描数据库,通过客户号和业务号读取客户信息.";
config.IsAuto = "true"; CustomerInfo ci1 = new CustomerInfo();
ci1.CustomerId = "0013";
ci1.BusinessId = "03";
CustomerInfo ci2 = new CustomerInfo();
ci2.CustomerId = "0022";
ci2.BusinessId = "02";
CustomerInfos cis = new CustomerInfos();
cis.cs = new CustomerInfo[] { ci1, ci2 }; config.CustomerInfos = cis; ScanConfigs sc = new ScanConfigs();
sc.BeginTime = "22:00:00";
sc.EndTimme = "23:00:00"; config.ScanConfigs = sc; XmlUtil.SerializeToXml(config, config.GetType(), "d:\\config.xml", null);

利用Xml架构生成实体访问类的更多相关文章

  1. 解析利用wsdl.exe生成webservice代理类的详解

    利用wsdl.exe生成webservice代理类:根据提供的wsdl生成webservice代理类1.开始->程序->Visual Studio 2005 命令提示2.输入如下红色标记部 ...

  2. 利用wsdl.exe生成webservice代理类

    通常要手动生成WebService代理类需要把一句生成语句,如 wsdl.exe /l:cs /out:D:\Proxy_UpdateService.cs  http://localhost:1101 ...

  3. generatorConfig.xml自动生成实体类,dao和xml

    <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfiguration ...

  4. VS2012 JSON、XML自动生成对应的类

    在VS编辑下拉框中,选择选择性粘贴(Paste Special)

  5. 利用org.mybatis.generator生成实体类

    springboot+maven+mybatis+mysql 利用org.mybatis.generator生成实体类 1.添加pom依赖:   2.编写generatorConfig.xml文件 ( ...

  6. 使用hibernate利用实体类生成表和利用表生成实体类

    1,配置数据库,这里以oracle数据库为例.点击右侧Database图标:

  7. NHibernate生成实体类、xml映射文件

    最近工作电脑装完win10后,之前使用的codeSmith安装不了,索性自己写一个. 界面比较简单,如下图: 第一行为Oracle数据库的连接字符串.连接成功后,填充表到第4行的下拉列表中. 第二行为 ...

  8. 利用JAXB实现java实体类和xml互相转换

    1.应用场景 在使用WebService实现数据上传下载,数据查询时,可以利用JAXB实现java实体类和xml互相转换 2.Demo 2.1 student.java 实体类,包含list(set同 ...

  9. 用vs2012的命令利用xsd文件生成对应的C#类,把xml的string类型映射到生成的类

    输入命令: xsd d:\TDDOWNLOAD\atom-author-link.xsd /c /language:C# /outputdir:d:\ 含义: 将d:\TDDOWNLOAD\atom- ...

随机推荐

  1. 守护进程,互斥锁,IPC,队列,生产者与消费者模型

    小知识点:在子进程中不能使用input输入! 一.守护进程 守护进程表示一个进程b 守护另一个进程a 当被守护的进程结束后,那么守护进程b也跟着结束了 应用场景:之所以开子进程,是为了帮助主进程完成某 ...

  2. undefined reference to `vtable for MyColor'

    MyColor是新建的类,原因是使用了QObject,但是系统没有反应过来 解决:从工程删除,再添加进去[QtCreator]

  3. maven项目出现红色感叹号报错

    背景 在eclipse部署maven项目的时候,项目出现红色的感叹号导致项目无法启动. 解决步骤 1.右键项目——>Maven——>Update Project ,弹出下框: 点击OK. ...

  4. 《编写高质量代码:Web 前端开发修炼之道》 笔记与读后感

    编写高质量代码:Web 前端开发修炼之道/曹刘阳著. —北京:机械工业出版社,2010.5 第一版 涉及到的知识点: 1. CSS Sprites 在国内很多人叫css精灵,是一种网页图片应用处理方式 ...

  5. 终于掌握vim的寄存器和系统剪贴板的使用了- 要安装vim-X11包

    vim的系统剪贴板 vim的 加号寄存器 "+ 是和系统剪贴板 相关联的. 加号寄存器和系统剪贴板之间的内容, 可以互相切换. 要把 加号寄存器中的内容, -> 放到/转移到系统剪贴板 ...

  6. Golang踩坑录 两种方式来读取文件一行所导致的问题

    前两天零零碎碎看完了golang的基础,想着找个小项目练练手,可是出现了一个十分棘手的问题 我要做的东西是网站路径爆破 所以我会从文本字典中把一行行路径读取然后与域名拼接,但是我在跑起程序后出现了问题 ...

  7. GDOI2018D2T1 谈笑风生

    T1 谈笑风生 [题目描述] [输入] [输出] 一行两个数,所需能量P与在能量最小的前提下最短的到达时间t. [样例输入] 5 7 66 4 3 2 1 5 1 2 1 5 2 3 2 4 2 5 ...

  8. 《计算机网络》-CCNA命令大全

    Router> //用户模式,只能简单的show及ping/tracer Router>enable //从用户模式进入特权模式 Router# //特权模式,能够进行所有的show及pi ...

  9. (转载)windows下安装配置Xampp

    XAMPP是一款开源.免费的网络服务器软件,经过简单安装后,就可以在个人电脑上搭建服务器环境.本文为大家介绍Windows中安装XAMPP(Apache+Mysql+PHP)及使用方法及其相关问题的总 ...

  10. Docker 开发概述

    This page lists resources for application developers using Docker. Develop new apps on Docker If you ...