转自 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. 南京大学发布无序列限制的DNA编辑新工具(转自生物通)

    编辑推荐: 内切酶经过改造可以成为强大的DNA编辑工具,比如ZFN.TALEN.风头正劲的CRISPR–Cas系统和充满争议的NgAgo技术.不过这些技术都是通过序列识别来实现靶向切割的,会受到序列偏 ...

  2. 64. Minimum Path Sum (Graph; DP)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  3. 不同包之间的继承extends

    情景如下: 两个类:ExtendsSuper(父类).ExtendsSub(子类) 两个包:父类ExtendsSuper位于包packSuper路径下,子类ExtendsSub位于包packSub路径 ...

  4. 关于GLSL中语法和调用规则的一些记录

    glsl是什么就不多说了.这里只介绍一下glsl中一些限定符. glsl中包含两类具有定义性质的符号,一类是和c++中定义变量的一样的符号,用来说明存放数据的类型,如float,int,bool.还有 ...

  5. win10 跳过max path 260限制

    参考: https://www.howtogeek.com/266621/how-to-make-windows-10-accept-file-paths-over-260-characters/ 注 ...

  6. pom.xml的继承、聚合与依赖

    原文地址:https://my.oschina.net/zh119893/blog/232896 6.1     简介 pom.xml文件是Maven进行工作的主要配置文件.在这个文件中我们可以配置M ...

  7. 【笔记】metasploit渗透测试魔鬼训练营-信息搜集

    exploit 漏洞利用代码 编码器模块:免杀.控制 help [cmd] msfcli适合对网络中大量系统统一测试. 打开数据包路由转发功能:/etc/sysctl.conf /etc/rc.loc ...

  8. [C#]创建Windows用户及组

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...

  9. nancyfx的安装笔记

    这个安装时很简单的 只要 Install-Package Nancy.Hosting.Aspnet 就行了. 需要注意的是,千万不要用那个模板安装,通过创建nancyfx类型项目的方式安装是有问题的. ...

  10. 随手记录: MVC自定义提交form

    function mySubmit() { var frm = $('#frm'); var result = frm.valid(); if (ret) { frm.submit(); } else ...