(转)使用VS实现XML2CS
转自 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的更多相关文章
随机推荐
- Shrio02 Realm作用、自定义简洁Realm、Realm实现类使用
1 Realm简介 1.1 Realm作用 shiro最终是通过Realm获取安全数据的(如用户.角色.权限),也就是说认证或者授权都会通过Realm进行数据操作 1.2 Realm接口 1.2.1 ...
- C#重启IIS
using System.Diagnostics; using System.ServiceProcess; //ServiceController sc1 = new ServiceControll ...
- code1105 过河
dp方程很简单: f[i] = min{ f[i-j] } + stone[i] 但是数据10^9太大了,超时超空间,这样只能过30% 来自:http://blog.csdn.net/w1996070 ...
- [SoapUI]怎样运用Schema通过*.xsd文件来验证response对应的xml文件
添加Groovy Script脚本对Test Step进行验证 脚本如下(已经运行通过): import javax.xml.XMLConstants import javax.xml.transfo ...
- c11时间库一个小例子
#pragma once #include <chrono> #include <string> #include <iostream> #include < ...
- gitweb配置
基于ssh的git服务器搭建可浏览:https://www.cnblogs.com/wswind/p/10373881.html 安装gitweb和apache yum -y install gitw ...
- chorme 浏览器记住密码后input黄色背景处理方法(两种)
使用chrome浏览器选择记住密码的账号,输入框会自动加上黄色的背景,有些设计输入框是透明背景的,需要去除掉这个黄色的背景: 方法1:阴影覆盖 input:-webkit-autofill { - ...
- Core Graphics Layer Drawing
[Core Graphics Layer Drawing] CGLayer objects (CGLayerRef data type) allow your application to use l ...
- win10自带虚拟机安装CentOS7系统(转)
出处:http://blog.csdn.net/bimabushihaodongxi/article/details/53677490 话说工欲善其事,必先利其器,在我准备学习Linux之前先要完成l ...
- scala高阶函数类型推断什么时候失效?
class TypeInfer(self: Int, other: Int) { def test(num: Int, word: String, fun1: (Int, Int) => Int ...