(转)使用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的更多相关文章
随机推荐
- js小例子之二级联动
联动原理 当用户点击省级的下拉选项,选择所在省,下一个下拉选项里的选项,则变成用户选择省下的所有市的信息,不会出现其它省市的信息. 省市数据 把省市数据,保存在js文件中,以json形式保存,以便读取 ...
- store下载文件保存位置
PC:C:\Users\accountName\AppData\Roaming\Unity\Asset Store MAC:"~/Library/Unity/Asset"
- bloom
bloom bloom也能实现和HDR类似的效果,但bloom的是静态的,HDR是动态渐变的, bloom在细节表现.明暗对比不如HDR,但实现HDR效果的系统资源开销也比 bloom大,bloom也 ...
- [Training Video - 4] [Selenium IDE]
Selenium IDE Training List
- selenium设置代理,基于chrome浏览器
工作中遇到需要对项目中使用的selenium设置代理,跟大家分享一下. 1.下载chromeDriver:http://chromedriver.storage.googleapis.com/inde ...
- 如何取得nginx做反向代理时的真实IP?
1. 编译 对于client -> nginx reverse proxy -> apache, 要想在程序中取得真实的IP,在执行nginx的configure时,必须指定参数" ...
- MySQL数据库Query性能定位
1.SQL前面加 EXPLAIN 定位到sql级别 各个属性的含义 id select查询的序列号 select_type select查询的类型,主要是区别普通查询和联合查询.子查询之类的复杂查询. ...
- Android-FileUtils工具类
文件相关工具类 public final class FileUtils { private FileUtils() { throw new UnsupportedOperationException ...
- 如何在CentOS 7中禁用IPv6
最近,我的一位朋友问我该如何禁用IPv6.在搜索了一番之后,我找到了下面的方案.下面就是在我的CentOS 7 迷你服务器关闭IPv6的方法. 你可以用两个方法做到这个. 方法 1 编辑文件/etc/ ...
- shell脚本参数中有空格
shell脚本参数中有空格 在shell脚本中如果有空格的处理如下: sh test.sh "hello word" echo $1 得到的是hello,而不是hello word ...