如何将XML / JSON文件转换为C#类? [英] How to convert XML/JSON file to C# class?
本文介绍了如何将XML / JSON文件转换为C#类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的 XML 这样的文件:
<?xml version="1.0"?>
<catalog>
<book id="1" date="2012-02-01">
<title>XML Developer's Guide</title>
<price>44.95</price>
<description>
An in-depth look at creating applications
with XML.
</description>
</book>
<book id="2" date="2013-10-16">
<author>Mark Colsberg</author>
<title>Dolor sit amet</title>
<price>5.95</price>
<description>Lorem ipsum</description>
</book>
</catalog>
如何快速将其转换为C#类通过LINQ使用访问数据? 我一定要手动编写的类的任何XML文件呢? 怎么样的 JSON 格式?
How to quick convert it to C# classes to use access data by LINQ? Do I have to write the class manually for any XML file case? What about JSON format?
时的XSD唯一的解决办法?
Is the XSD the only solution?
推荐答案
您有两种可能性。
假设你有你的XML文件中的这个位置
C:\路径\到\ XML \ file.xml
Suppose that you have your XML file in this location
C:\path\to\xml\file.xml
- 开启开发命令提示符
你可以找到它在开始菜单&gt;程序&gt;微软的Visual Studio 2012&GT; Visual Studio工具
或者,如果您有Windows 8可以开始在输入开发命令提示符 开始屏幕的 - 键入
CD / DC:\路径\为\ XML更改位置,您的XML文件的目录
- 创建 XSD文件从XML文件通过键入
XSD file.xml
- 创建 C#类键入
XSD / C file.xsd
- Open Developer Command Prompt
You can find it inStart 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
这就是它!您已经生成的XML文件中的C#类 C:\路径\到\ XML \ file.cs
And that's it! You have generated C# classes from xml file in C:\path\to\xml\file.cs
所需的Visual Studio 2012 +
Required Visual Studio 2012+
- 在复制内容的XML文件到剪贴板
- 添加到您的解决方案新的,空的类文件(移骨节病> + <大骨节病>替代骨节病> + <大骨节病> C 骨节病>)
- 打开该文件,并在菜单中点击
编辑&gt;选择性粘贴&GT;粘贴XML作为类
- 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 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(@"C:\path\to\xml\file.xml");
var catalog1 = xml.ParseXML<catalog>();
string json = File.ReadAllText(@"C:\path\to\json\file.json");
var catalog2 = json.ParseJSON<JSONRoot>();
在这里,你有一些在线 XML&LT; - &GT; JSON
转换器:点击
这篇关于如何将XML / JSON文件转换为C#类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文