转自 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. Efficient algorithms for polyploid haplotype phasing 多倍体单体型分型的有效算法

    背景:单倍型的推断,或沿着相同染色体的等位基因序列,是遗传学中的基本问题,并且是许多分析的关键组分,包括混合物图谱,通过下降和插补识别身份区域. 基于测序读数的单倍型定相引起了很多关注. 已经广泛研究 ...

  2. Trait 概览

    Trait是PHP 5.4引入的新概念,看上去既像类又像接口,其实都不是,Trait可以看做类的部分实现,可以混入一个或多个现有的PHP类中,其作用有两个:表明类可以做什么:提供模块化实现.Trait ...

  3. MVC5数据库迁移命令!

    首先数据库迁移在上下文里设置要设置成为CreateDatabaseIfNotExists, 然后在Nuget控制平台输入命令 在“程序包管理器控制台”窗口中输入:Enable-Migrations   ...

  4. java可视化

    1.java关闭窗口代码. ft.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); IE打开  Weiler-Atherton任意多边形裁剪 http:/ ...

  5. Text Relatives II

    [Text Relatives II] When your app determines that the user has requested the edit menu—which could b ...

  6. UVaLive 4128 Steam Roller (多决策最短路)

    题意:给定一个图,r 根横线, c 根竖线.告诉你起点和终点,然后从起点走,每条边有权值,如果是0,就表示无法通行.走的规则是:如果你在下个路要转弯,会使这段路的时间加倍,但是如果一条路同时是这样,那 ...

  7. Intel Cyclone SoC FPGA介绍

    3.1 Intel Cyclone SoC FPGA介绍 3.1.1 SoC FPGA的基本概念 Intel Cyclone V SoC FPGA是Intel PSG(原Altera)于2013年发布 ...

  8. ORACLE PATCH 版本的查询 PL/SQL

    --ORACLE PATCH 版本的查询 PL/SQL SELECT DD.PATCH_NAME,        PP.CREATION_DATE,        PP.DRIVER_FILE_NAM ...

  9. django设置cookie和session

    1 设置cookie 本例中应用名称为cookie 模型model from django.db import models from django.db import models class Us ...

  10. View Pi's Status on WebBrowser

    1. install php and cgi support sudo apt-get install php5-common sudo apt-get install php5-cgi sudo a ...