转自 StackOverFlow

Method 1 XSD tool

Suppose that you have your XML file in this location C:\path\to\xml\file.xml

  • Open Developer Command Prompt

    You can find it in Start Menu > Programs > Microsoft Visual Studio 2012 > Visual Studio Tools Or if you have Windows 8 can just start typing Developer Command Prompt in Start screen
  • Change location to your XML file directory by typing cd /D "C:\path\to\xml"
  • Create XSD file from your xml file by typing xsd file.xml
  • Create C# classes by typing xsd /c file.xsd

    And that's it! You have generated C# classes from xml file in C:\path\to\xml\file.cs

Method 2 Paste special

  • Copy content of your XML file to clipboard
  • Add to your solution new, empty class file (Shift+Alt+C)
  • Open that file and in menu click Edit > Paste special > Paste XML As Classes

Usage

  • Usage is very simple with this helper class
using System;
using System.IO;
using System.Web.Script.Serialization; // Add reference: System.Web.Extensions
using System.Xml;
using System.Xml.Serialization; namespace Helpers
{
internal static class ParseHelpers
{
private static JavaScriptSerializer json;
private static JavaScriptSerializer JSON { get { return json ?? (json = new JavaScriptSerializer()); } } public static Stream ToStream(this string @this)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(@this);
writer.Flush();
stream.Position = 0;
return stream;
} public static T ParseXML<T>(this string @this) where T : class
{
var reader = XmlReader.Create(@this.Trim().ToStream(), new XmlReaderSettings() { ConformanceLevel = ConformanceLevel.Document });
return new XmlSerializer(typeof(T)).Deserialize(reader) as T;
} public static T ParseJSON<T>(this string @this) where T : class
{
return JSON.Deserialize<T>(@this.Trim());
}
}
}
  • All you have to do now, is:
 public class JSONRoot
{
public catalog catalog { get; set; }
}
string xml = File.ReadAllText(@"D:\file.xml");
var catalog1 = xml.ParseXML<catalog>(); string json = File.ReadAllText(@"D:\file.json");
var catalog2 = json.ParseJSON<JSONRoot>();

(转)使用VS实现XML2CS的更多相关文章

随机推荐

  1. Spring IOC容器启动流程源码解析(一)——容器概念详解及源码初探

    目录 1. 前言 1.1 IOC容器到底是什么 1.2 BeanFactory和ApplicationContext的联系以及区别 1.3 解读IOC容器启动流程的意义 1.4 如何有效的阅读源码 2 ...

  2. 14.Longest Common Prefix (String)

    Write a function to find the longest common prefix string amongst an array of strings. class Solutio ...

  3. iOS正确解决隐藏导航栏后push和pop或dismiss和present闪黑问题

    情景: 一级页面不显示导航栏 ,二级页面显示导航栏. 方法一 适用于push/pop: 一级页面中 - (void)viewWillAppear:(BOOL)animated { [super vie ...

  4. 基于Dcoker的ZooKeeper集群的搭建

    背景 原来学习 ZK 时, 我是在本地搭建的伪集群, 虽然说使用起来没有什么问题, 但是总感觉部署起来有点麻烦. 刚好我发现了 ZK 已经有了 Docker 的镜像了, 于是就尝试了一下, 发现真是爽 ...

  5. SQLSERVER CROSS APPLY 与 OUTER APPLY 的应用

    日常开发中遇到多表查询时,首先会想到 INNER JOIN 或 LEFT OUTER JOIN 等等,但是这两种查询有时候不能满足需求.比如,左表一条关联右表多条记录时,我需要控制右表的某一条或多条记 ...

  6. radiobutton 选中的项不能去掉选择的问题

    代码如下: RadioButton rbtn = new RadioButton(getApplicationContext()); rbtn.setText(String.valueOf(item. ...

  7. xml数据改动

    public void reXml ( string namepngname ) { XmlDocument doc = new XmlDocument(); doc.Load(_xmlpath); ...

  8. http://angular.github.io/router/

    Angular New Router Guide Configuring the Router- This guide shows the many ways to map URLs to compo ...

  9. win7搭建node+npm+bower的环境

    原文的地址:https://my.oschina.net/JeeChou/blog/219699 Windows下的NodeJS安装是比较方便的(v0.6.0版本之后,支持windows native ...

  10. Linux 基础教程 44-history命令

    什么是history     在Linux系统日积月累的使用中,我们会输入很多命令.而在我们想重复上一个命令时,通过使用方向键向上翻就可以查看我们已经输入和使用过的命令.那大家有没有想过这个命令保存在 ...