XML反序列化

XML反序列化

问题描述:

我有一些来自调查系统的XML。示例结构如下所示:

I have some XML from survey system. Sample structure is shown below:

<?xml version="1.0" encoding="windows-1250" standalone="yes" ?>
<document>
 <ID>100</ID>
 <DOCUMENT_DATA>
  <OWNER>SOME OWNER</OWNER>
  <CODING>WINDOWS-1250</CODING>
  <MAIN_DATA>
   <NAME>JOHN</NAME>
   <SURNAME>DOE</SURNAME>
   <SYSTEM_ID>000</SYSTEM_ID>
   <COUNTRY>GB</COUNTRY>
  </MAIN_DATA>
  <SUB_DATA>
   <STREET>SOME STREET</STREET>
   <BUILDING_NO>120</BUILDING_NO>
   <FLAT_NO>200</FLAT_NO>
   <CITY>LONDON</CITY>
  </SUB_DATA>
  <DESCRIPTION>
   <HAIR>BLACK</HAIR>
   <EYES>BROWN</EYES>
   <BODY>SLIM</BODY>
   <HEIGHT>176</HEIGHT>
   <STATUS>
    <STATUS_NAME>SINGLE</STATUS_NAME>
   </STATUS>
   <SEX>MALE</SEX>
   <TATOO>NO</TATOO>
   <PIERCING>NO</PIERCING>
  </DESCRIPTION>
  <INTEREST>
   <GENERAL>
    <SPORT>
     <MAIN>SURFING</MAIN>
     <TRAINING>EVERY DAY</TRAINING>
    </SPORT>
    <CARS>CLASSIC</CARS>
    <OTHER>MUSIC</OTHER>
   </GENERAL>
   <BOOKS>CRIMINALS</BOOKS>
   <MOVIES>THRILLER</MOVIES>
  </INTEREST>
 </DOCUMENT_DATA>
</document>

我的任务是使用System.XML.Serialization读取XML文件并将其反序列化为C#中的对象。引用和标志,例如[XmlAttribute],[XmlElement]等。
我不熟悉这种技术,因为我通常对DataSet类型使用标准的快速反序列化。
有人可以帮我完成此任务吗?

My task is to read XML file and deserialize it to objects in C#, using System.XML.Serialization reference and flags like [XmlAttribute], [XmlElement] and so on. I'm not familiar with this technique, because I usually use standard and fast deserialization to DataSet type. Can somebody help me with this task?

要快速修复,可以在Visual Studio中创建一个新类。 ,将XML复制到剪贴板,然后选择编辑,特殊粘贴,将XML粘贴为类。

For a quick fix you can create a new class in Visual Studio, copy your XML to the clipboard and then choose "Edit", "Paste Special", "Paste XML as classes".

这将自动生成一个类,您可以然后用于序列化/反序列化。

This will auto generate a class which you can then use for serialization/deserialization.

但是,正如@Tdorno所建议的那样,网站上有很多有用的示例,例如: C#反序列化xml文件

However, as @Tdorno suggests, there are plenty of helpful examples on the site, e.g.: C# Deserialization xml file