转自 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. codeforces:MEX Queries分析和实现

    首先说明一下MEX,设S是自然数集合N的一个子集,那么S的MEX则为min(N\S),即不包含于S的最小自然数. 题目大意是存在一个空集S,提供n组输入(n<10^5),每组输入对应下面的一个指 ...

  2. 修改 Windows 10 UWP 应用任务栏图标

    修改 Windows 10 UWP 应用任务栏图标 Windows 7 时代,修改任务栏图标很简单,右键打开属性,更改图标即可.但步入 Windows 8 之后,随着应用商店 UWP 应用的问世,可以 ...

  3. 使用透明flash+背景图片制作绚丽页面

    关键代码: <div style="width: 469px; height: 303px; background-image: url('https://images0.cnblog ...

  4. JavaScript事件 DOMNodeInserted DOMNodeRemoved

    JavaScript与HTML之间的交互是通过事件实现的.事件,就是文档或浏览器窗口中发生的一些特定交互的瞬间.可以使用侦听器(或处理程序)来预订事件,以便事件发生时执行相应的代码. 13.1 事件流 ...

  5. Linux中bash编程

    bash编程也叫shell编程 预定义变量         $? 最后一次执行的命令的返回状态.如果这个变量的值为0,证明上一个命令正确的执行:如果这个变量返回的值非0(具体是那个数,有命令自己来决定 ...

  6. 不要怂,就是GAN (生成式对抗网络) (五):无约束条件的 GAN 代码与网络的 Graph

    GAN 这个领域发展太快,日新月异,各种 GAN 层出不穷,前几天看到一篇关于 Wasserstein GAN 的文章,讲的很好,在此把它分享出来一起学习:https://zhuanlan.zhihu ...

  7. mongo学习- group操作

    group可以使用 $sum,$avg,$max,$min,$first,$last

  8. mysql event 入门

    delimiter | CREATE EVENT statistics_event ON SCHEDULE EVERY DAY STARTS CONCAT(CURRENT_DATE(), ' 00:0 ...

  9. Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks(使用循环一致的敌对网络进行不成对的图像到图像转换)

    作者:朱俊彦,朱俊彦博士是计算机图形学领域现代机器学习应用的开拓者.他的论文可以说是第一篇用深度神经网络系统地解决自然图像合成问题的论文.因此,他的研究对这个领域产生了重大影响.他的一些科研成果,尤其 ...

  10. nginx 配置中的if判断

    正则表达式匹配:     ==:等值比较;     ~:与指定正则表达式模式匹配时返回“真”,判断匹配与否时区分字符大小写:     ~*:与指定正则表达式模式匹配时返回“真”,判断匹配与否时不区分字 ...