在 C# 中反序列化 XML [重复]
Posted
技术标签:
【中文标题】在 C# 中反序列化 XML [重复]【英文标题】:Deserialise XML in C# [duplicate] 【发布时间】:2013-04-28 03:44:35 【问题描述】:我有以下 XML
<getAvailability>
<result>
<arrival_date>2013-05-05</arrival_date>
<block>
<block_id>80884788</block_id>
</block>
<id>230802</id>
</result>
<result>
<arrival_date>2013-05-05</arrival_date>
<block>
<block_id>419097</block_id>
</block>
<id>98121</id>
</result>
</getAvailability>
如何反序列化它并将其放入 C# 中的类中?非常感谢。另外这里是我的代码;
XmlSerializer serializer = new XmlSerializer(typeof(getAvailability));
getAvailability output;
using (StringReader reader = new StringReader(xmlSource))
output = (getAvailability)serializer.Deserialize(reader);
public class Result
[XmlElement("block_id")]
public string sBlockID get; set;
[XmlElement("arrival_date")]
public DateTime dArrivalDate get; set;
[XmlElement("id")]
public int iID get; set;
[XmlRoot("getAvailability")]
public class getAvailability
[XmlArray("result")]
[XmlArrayItem("block", typeof(Result))]
public Result[] Result get; set;
如果您需要任何进一步的信息,请告诉我。
【问题讨论】:
这里似乎有人问了一个很好的答案***.com/questions/364253/… 我按照***.com/questions/364253/… 的示例进行操作,但由于某种原因,我的程序只读取了 XML 文件的最后一个元素 你能发布一个行为不端的小示例程序吗? 刚刚用代码更新了我原来的帖子 【参考方案1】:最简单的方法是按照下面的代码示例构建结果类,以便轻松地将其映射到您提供的 XML 示例。我添加了一些提供有关某些方法的信息的 cmets。
using System;
using System.IO;
using System.Xml.Serialization;
namespace Test
public class Program
public static void Main(string[] args)
Result result1 = new Result
arrival_date = new DateTime(2013, 05, 05),
block = new Result.Block block_id = 80884788 ,
id = 230802
;
Result result2 = new Result
arrival_date = new DateTime(2013, 05, 05),
block = new Result.Block block_id = 419097 ,
id = 98121
;
Results results = new Results result = new Result[2] ;
results.result[0] = result1;
results.result[1] = result2;
WriteSettingsAsXml("D:\\test.xml", typeof(Results), results, true);
Results gA = (Results)ReadSettingsFromXml("D:\\test.xml", typeof(Results));
// This `Result` class below maps to a single result in the XML you provided. This class is used in the `Results` class to obtain the needed XML structure.
public class Result
public class Block
public Int32 block_id get; set;
public DateTime arrival_date get; set;
public Block block get; set;
public Int32 id get; set;
[Serializable()]
[XmlRootAttribute("getAvailability", Namespace = "", DataType = "", IsNullable = false)]
public class Results
[XmlElement("result")]
public Result[] result get; set;
/// Library methods that saves/reads any passed/retrieved object into/from a xml file at specified location
public static void WriteSettingsAsXml(string destinationPath, Type objectType, object objectValue, bool hideNamespaces)
XmlSerializer serializer = new XmlSerializer(objectType);
using (TextWriter writer = new StreamWriter(destinationPath))
if (hideNamespaces)
XmlSerializerNamespaces hiddenNamespaces = new XmlSerializerNamespaces();
hiddenNamespaces.Add("", "");
serializer.Serialize(writer, objectValue, hiddenNamespaces);
else
serializer.Serialize(writer, objectValue);
writer.Close();
public static object ReadSettingsFromXml(string xmlFilePath, Type objectType)
XmlSerializer serializer = new XmlSerializer(objectType);
using (FileStream fileStream = new FileStream(xmlFilePath, FileMode.Open))
return serializer.Deserialize(fileStream);
【讨论】:
感谢它完美运行。 我用下面的例子给出了两个元素 '以上是关于在 C# 中反序列化 XML [重复]的主要内容,如果未能解决你的问题,请参考以下文章
C# Restful WCF 服务。无法在帖子正文中反序列化 XML