Ale*_*lex 453 c# xml asp.net serialization xml-deserialization
如何反序列化此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>
Run Code Online (Sandbox Code Playgroud)
我有这个:
[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; }
}
Run Code Online (Sandbox Code Playgroud)
.
[System.Xml.Serialization.XmlRootAttribute("Cars", Namespace = "", IsNullable = false)]
public class Cars
{
[XmlArrayItem(typeof(Car))]
public Car[] Car { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
似乎不起作用:-(
Mar*_*ell 430
你如何将xml保存到文件中,并使用xsd生成C#类?
xsd foo.xml
xsd foo.xsd /classes
Et voila - 和C#代码文件应该能够通过XmlSerializer
以下方式读取数据:
XmlSerializer ser = new XmlSerializer(typeof(Cars));
Cars cars;
using (XmlReader reader = XmlReader.Create(path))
{
cars = (Cars) ser.Deserialize(reader);
}
Run Code Online (Sandbox Code Playgroud)
(包括项目中生成的foo.cs)
Kev*_*ghe 344
这是一个工作版本.我将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; }
}
Run Code Online (Sandbox Code Playgroud)
反序列化功能:
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();
Run Code Online (Sandbox Code Playgroud)
稍微调整一下的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>
Run Code Online (Sandbox Code Playgroud)
Dam*_*iel 226
你有两种可能性.
C:\path\to\xml\file.xml
Start Menu > Programs > Microsoft Visual Studio 2012 > Visual Studio Tools
或中找到它.如果您有Windows 8,只需在" 开始"屏幕中键入开发人员命令提示符即可cd /D "C:\path\to\xml"
xsd file.xml
xsd /c file.xsd
就是这样!您已从xml文件生成C#类C:\path\to\xml\file.cs
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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
你现在要做的就是:
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>();
Run Code Online (Sandbox Code Playgroud)
小智 83
以下代码段应该可以解决问题(您可以忽略大多数序列化属性):
public class Car
{
public string StockNumber { get; set; }
public string Make { get; set; }
public string Model { get; set; }
}
[XmlRootAttribute("Cars")]
public class CarCollection
{
[XmlElement("Car")]
public Car[] Cars { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
...
using (TextReader reader = new StreamReader(path))
{
XmlSerializer serializer = new XmlSerializer(typeof(CarCollection));
return (CarCollection) serializer.Deserialize(reader);
}
Run Code Online (Sandbox Code Playgroud)
Joe*_*orn 23
看看这是否有帮助:
[Serializable()]
[System.Xml.Serialization.XmlRootAttribute("Cars", Namespace = "", IsNullable = false)]
public class Cars
{
[XmlArrayItem(typeof(Car))]
public Car[] Car { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
.
[Serializable()]
public class Car
{
[System.Xml.Serialization.XmlElement()]
public string StockNumber{ get; set; }
[System.Xml.Serialization.XmlElement()]
public string Make{ get; set; }
[System.Xml.Serialization.XmlElement()]
public string Model{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)
并且没有使用visual studio附带的xsd.exe程序来创建基于该xml文件的模式文档,然后再次使用它来创建基于模式文档的类.
小智 9
我不认为.net是"对反序列化数组的挑剔".第一个xml文档格式不正确.没有根元素,虽然它看起来像.规范的xml文档具有根和至少1个元素(如果有的话).在你的例子中:
<Root> <-- well, the root
<Cars> <-- an element (not a root), it being an array
<Car> <-- an element, it being an array item
...
</Car>
</Cars>
</Root>
Run Code Online (Sandbox Code Playgroud)
如果您的.xml文件已在磁盘中的某个位置生成并且您已使用过,请尝试此代码块List<T>
:
//deserialization
XmlSerializer xmlser = new XmlSerializer(typeof(List<Item>));
StreamReader srdr = new StreamReader(@"C:\serialize.xml");
List<Item> p = (List<Item>)xmlser.Deserialize(srdr);
srdr.Close();`
Run Code Online (Sandbox Code Playgroud)
注意:C:\serialize.xml
是我的.xml文件的路径.您可以根据需要进行更改.
除了事实之外,Kevin的anser很好,在现实世界中,您通常无法改变原始XML以满足您的需求.
原始XML也有一个简单的解决方案:
[XmlRoot("Cars")]
public class XmlData
{
[XmlElement("Car")]
public List<Car> Cars{ get; set; }
}
public class Car
{
public string StockNumber { get; set; }
public string Make { get; set; }
public string Model { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后你可以简单地打电话:
var ser = new XmlSerializer(typeof(XmlData));
XmlData data = (XmlData)ser.Deserialize(XmlReader.Create(PathToCarsXml));
Run Code Online (Sandbox Code Playgroud)
我发现这里的答案非常有帮助,也就是说我仍然努力(只是有点)让这个工作。因此,如果它对某人有帮助,我将详细说明工作解决方案:
来自原始问题的 XML。xml在文件Class1.xml中,path
代码中使用了这个文件来定位这个xml文件。
我使用@erymski 的答案来解决这个问题,因此创建了一个名为 Car.cs 的文件并添加了以下内容:
Run Code Online (Sandbox Code Playgroud)using System.Xml.Serialization; // Added public class Car { public string StockNumber { get; set; } public string Make { get; set; } public string Model { get; set; } } [XmlRootAttribute("Cars")] public class CarCollection { [XmlElement("Car")] public Car[] Cars { get; set; } }
@erymski 提供的另一段代码......
Run Code Online (Sandbox Code Playgroud)using (TextReader reader = new StreamReader(path)) { XmlSerializer serializer = new XmlSerializer(typeof(CarCollection)); return (CarCollection) serializer.Deserialize(reader); }
...进入您的主程序(Program.cs),static CarCollection XCar()
如下所示:
using System;
using System.IO;
using System.Xml.Serialization;
namespace ConsoleApp2
{
class Program
{
public static void Main()
{
var c = new CarCollection();
c = XCar();
foreach (var k in c.Cars)
{
Console.WriteLine(k.Make + " " + k.Model + " " + k.StockNumber);
}
c = null;
Console.ReadLine();
}
static CarCollection XCar()
{
using (TextReader reader = new StreamReader(@"C:\Users\SlowLearner\source\repos\ConsoleApp2\ConsoleApp2\Class1.xml"))
{
XmlSerializer serializer = new XmlSerializer(typeof(CarCollection));
return (CarCollection)serializer.Deserialize(reader);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你 :-)
小智 5
尝试使用此通用类进行Xml序列化和反序列化。
public class SerializeConfig<T> where T : class
{
public static void Serialize(string path, T type)
{
var serializer = new XmlSerializer(type.GetType());
using (var writer = new FileStream(path, FileMode.Create))
{
serializer.Serialize(writer, type);
}
}
public static T DeSerialize(string path)
{
T type;
var serializer = new XmlSerializer(typeof(T));
using (var reader = XmlReader.Create(path))
{
type = serializer.Deserialize(reader) as T;
}
return type;
}
}
Run Code Online (Sandbox Code Playgroud)
使用泛型类来反序列化 XML 文档怎么样?
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Generic class to load any xml into a class
// used like this ...
// YourClassTypeHere InfoList = LoadXMLFileIntoClass<YourClassTypeHere>(xmlFile);
using System.IO;
using System.Xml.Serialization;
public static T LoadXMLFileIntoClass<T>(string xmlFile)
{
T returnThis;
XmlSerializer serializer = new XmlSerializer(typeof(T));
if (!FileAndIO.FileExists(xmlFile))
{
Console.WriteLine("FileDoesNotExistError {0}", xmlFile);
}
returnThis = (T)serializer.Deserialize(new StreamReader(xmlFile));
return (T)returnThis;
}
Run Code Online (Sandbox Code Playgroud)
这部分可能是必要的,也可能不是必要的。在 Visual Studio 中打开 XML 文档,右键单击 XML,选择属性。然后选择您的架构文件。
小智 5
一个班轮:
var object = (Cars)new XmlSerializer(typeof(Cars)).Deserialize(new StringReader(xmlString));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
635004 次 |
最近记录: |