如何反序列化XML文档
我如何反序列化这个XML文档:
<?xml version="1.0" encoding="utf-8"?>
<Cars>
<Car>
<StockNumber>1020</StockNumber>
<Make>Nissan</Make>
<Model>Sentra</Model>
</Car>
<Car>
<StockNumber>1010</StockNumber>
<Make>Toyota</Make>
<Model>Corolla</Model>
</Car>
<Car>
<StockNumber>1111</StockNumber>
<Make>Honda</Make>
<Model>Accord</Model>
</Car>
</Cars>
我有这个:
[Serializable()]
public class Car
{
[System.Xml.Serialization.XmlElementAttribute("StockNumber")]
public string StockNumber{ get; set; }
[System.Xml.Serialization.XmlElementAttribute("Make")]
public string Make{ get; set; }
[System.Xml.Serialization.XmlElementAttribute("Model")]
public string Model{ get; set; }
}
。
[System.Xml.Serialization.XmlRootAttribute("Cars", Namespace = "", IsNullable = false)]
public class Cars
{
[XmlArrayItem(typeof(Car))]
public Car[] Car { get; set; }
}
。
public class CarSerializer
{
public Cars Deserialize()
{
Cars[] cars = null;
string path = HttpContext.Current.ApplicationInstance.Server.MapPath("~/App_Data/") + "cars.xml";
XmlSerializer serializer = new XmlSerializer(typeof(Cars[]));
StreamReader reader = new StreamReader(path);
reader.ReadToEnd();
cars = (Cars[])serializer.Deserialize(reader);
reader.Close();
return cars;
}
}
那似乎不起作用:-(
这是一个工作版本。 我将XmlElementAttribute标签更改为XmlElement,因为在xml中StockNumber,Make和Model值是元素,而不是属性。 我也删除了reader.ReadToEnd(); (该函数读取整个流并返回一个字符串,因此Deserialze()函数不能再使用阅读器了......位置在流的末尾)。 我也采取了一些自由与命名:)。
这里是类:
[Serializable()]
public class Car
{
[System.Xml.Serialization.XmlElement("StockNumber")]
public string StockNumber { get; set; }
[System.Xml.Serialization.XmlElement("Make")]
public string Make { get; set; }
[System.Xml.Serialization.XmlElement("Model")]
public string Model { get; set; }
}
[Serializable()]
[System.Xml.Serialization.XmlRoot("CarCollection")]
public class CarCollection
{
[XmlArray("Cars")]
[XmlArrayItem("Car", typeof(Car))]
public Car[] Car { get; set; }
}
反序列化功能:
CarCollection cars = null;
string path = "cars.xml";
XmlSerializer serializer = new XmlSerializer(typeof(CarCollection));
StreamReader reader = new StreamReader(path);
cars = (CarCollection)serializer.Deserialize(reader);
reader.Close();
稍微调整的xml(我需要添加一个新元素来包装<Cars> ... Net对反序列化数组有挑剔):
<?xml version="1.0" encoding="utf-8"?>
<CarCollection>
<Cars>
<Car>
<StockNumber>1020</StockNumber>
<Make>Nissan</Make>
<Model>Sentra</Model>
</Car>
<Car>
<StockNumber>1010</StockNumber>
<Make>Toyota</Make>
<Model>Corolla</Model>
</Car>
<Car>
<StockNumber>1111</StockNumber>
<Make>Honda</Make>
<Model>Accord</Model>
</Car>
</Cars>
</CarCollection>
你如何将xml保存到一个文件中,并使用xsd?
xsd foo.xml
xsd foo.xsd /classes
Et voila - 以及应该能够通过XmlSerializer
读取数据的C#代码文件:
XmlSerializer ser = new XmlSerializer(typeof(Cars));
Cars cars;
using (XmlReader reader = XmlReader.Create(path))
{
cars = (Cars) ser.Deserialize(reader);
}
(在项目中包含生成的foo.cs)
你有两种可能性。
方法1. XSD工具
假设您的XML文件位于此位置
C:pathtoxmlfile.xml
您可以在
Start Menu > Programs > Microsoft Visual Studio 2012 > Visual Studio Tools
找到它或者如果您有Windows 8,只需在开始屏幕中输入Developer Command Prompt cd /D "C:pathtoxml"
位置更改为XML文件目录 xsd file.xml
从xml文件创建XSD文件 xsd /c file.xsd
创建C#类 就是这样! 您已经从C:pathtoxmlfile.cs
xml文件生成C#类C:pathtoxmlfile.cs
方法2 - 特殊粘贴
必需的Visual Studio 2012+
Edit > Paste special > Paste XML As Classes
就是这样!
用法
这个辅助类的用法非常简单:
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());
}
}
}
你现在要做的就是:
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>();
链接地址: http://www.djcxy.com/p/65997.html
上一篇: How to Deserialize XML document
下一篇: ClickOnce include imported .targets when publishing (FFMPEG)