convert-xml-to-csharp-classes/
来源:互联网 发布:办公软件的基础知识 编辑:程序博客网 时间:2024/06/13 05:57
refs:
https://dennymichael.net/2014/05/30/convert-xml-to-csharp-classes/comment-page-1/
Today I found a cool Visual Studio 2012/2013 functionality: you can paste an XML source as Classes, in fact creating all the object model to serialize and deserialize object with the xml format, all this without using xsd.exe tool.
Here’s the very simple steps:
1 – The most difficult step….. copy the xml source in the clipboard, something like CTRL+A and CTRL+C
Is ridiculous to add a screenshot, but I’ve got it, so why not!
2 – Create a new empy class file… no more screenshot please! ok here we go
3 – Go to Edit -> Paste Special -> Paste XML As Classes, to paste the generated classes based on the source xml
Here’s the code I’ve used to test the deserialization:
using
System;
using
System.IO;
using
System.Text;
using
System.Xml.Serialization;
using
ConsoleDump;
using
ConvertXmlToCSharpClasses.Properties;
namespace
ConvertXmlToCSharpClasses
{
internal
class
Program
{
private
static
void
Main(
string
[] args)
{
TestSample1();
TestSample2();
Console.WriteLine(
"Press enter to exit the application..."
);
Console.ReadLine();
}
private
static
void
TestSample1()
{
var
serializer =
new
XmlSerializer(
typeof
(library));
var
buffer = Encoding.UTF8.GetBytes(Resources.Sample1);
using
(
var
stream =
new
MemoryStream(buffer))
{
var
library = (library)serializer.Deserialize(stream);
library.book.Dump(
"Book"
);
library.book.title.Dump(
"Book Title"
);
library.book.author.Dump(
"Book Title"
);
}
}
private
static
void
TestSample2()
{
var
serializer =
new
XmlSerializer(
typeof
(catalog));
var
buffer = Encoding.UTF8.GetBytes(Resources.Sample2);
using
(
var
stream =
new
MemoryStream(buffer))
{
var
catalog = (catalog)serializer.Deserialize(stream);
catalog.product.Dump(
"Product"
).catalog_item.Dump(
"Product Items"
)[0].size.Dump(
"Item Size"
)[0].color_swatch.Dump(
"Color Swatch"
);
}
}
}
}
You can also download the test project.
4 – Enjoy your saved time
- convert-xml-to-csharp-classes/
- csharp: word or excel Convert to PDF
- csharp: word or excel Convert to PDF
- string Convert To XML
- xml Convert To ArrayCollection
- xml convert to excel
- Convert excel to xml
- Convert xml string to obj
- CommandInvokationFailure: Unable to convert classes into dex format解决方法
- Unable to convert classes into dex format 解决方案之一
- http://www.developerfusion.com/tools/convert/csharp-to-vb/ 将C转换成vb语言
- Csharp:The .dat File using BinaryReader and BinaryWriter Convert to DataTable
- Csharp:The .dat File using BinaryReader and BinaryWriter Convert to DataTable
- Csharp:The .dat File using BinaryReader and BinaryWriter Convert to DataTable
- href of python convert HTML to XML
- SQL 2008 Convert Xml to Table
- Convert Xml to json object in android
- java jaxbContext convert bean to xml
- python SMTP 550 错误
- 关于数组地址问题
- centos 7 中防火墙安装、启动和关闭
- Android 学习资源大全集汇总(github持续更新)
- 协同过滤推荐算法总结
- convert-xml-to-csharp-classes/
- iOS UIButton 高亮变灰的效果去掉 的方法
- Android 开发所有权限解析
- 有点小小泄气
- VS2012 ASP.NET 母版页的创建和使用
- PHP中this,self,parent的区别
- Android 线程 thread
- WCF_自宿主服务
- React Native开发总结