C# 序列化简单格式XML

问师傅反序列化和序列化到底是什么,

然后师傅鄙视一下我的智商,让我做个反序列化解析XML。

一边听着师傅在旁边跟女朋友打电话收到暴击伤害,一边写,搞了一个半小时。

 

XML文件:

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 
 3 <PRODUCT>
 4   
 5 <SatelliteId>TERRA</SatelliteId>
 6  
 7 <SensorId>MODIS</SensorId>
 8   
 9 <ProductId>HAZ</ProductId>
10   
11 <!--Inversion Combine-->
12   
13 <ProductType>Inversion</ProductType>
14   
15 <!--输入参数为单或多个文件(文件以;间隔,NPP数据为文件夹)-->
16   <InputDataFile>/DPS/Data/xxx.hdf</InputDataFile>
17   
18 <InversionArgs>
19     
20 <!--参数列表-->
21     
22 <InversionArg name="" value=""/>
23   
24 </InversionArgs>
25   
26 <!--输出结果及日志文件目录-->
27   
28 <OutputDataFilePath>/DPS/Data/L2/PRD/</OutputDataFilePath>
29   
30 <OutputDataFileList>
31     
32 <OutputDataFile name="" value="Hxxx.hdf" />
33     
34 <OutputDataFile name="" value="Hxx.hdf" />
35     
36 <OutputDataFile name="" value="Hxxx.hdf"/>
37   
38 </OutputDataFileList>
39   
40 <LogInfoFile>xxxxx.log</LogInfoFile>
41 
42 </PRODUCT> 

 

 

类与调用代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Xml.Serialization;
 7 
 8 namespace ConsoleApplication1
 9 {
10     [Serializable()]
11     public class PRODUCT
12     {
13         public PRODUCT() { }
14         public string SatelliteId { get; set; }
15         public string SensorId { get; set; }
16         public string ProductId { get; set; }
17         public string ProductType { get; set; }
18         public string InputDataFile { get; set; }
19         
20         [XmlArray("InversionArgs")]
21         [XmlArrayItem("InversionArg", typeof(namevlue))]
22         public List<namevlue> InversionArgs { get; set; }
23         public string OutputDataFilePath { get; set; }
24         
25         [XmlArray("OutputDataFileList")]
26         [XmlArrayItem("OutputDataFile", typeof(namevlue))]
27         public List<namevlue> OutputDataFileList { get; set; }
28         public string LogInfoFile { get; set; }
29     }
30 
31     [Serializable()]
32     public class namevlue
33     {
34         [XmlAttribute]
35         public string name { get; set; }
36         [XmlAttribute]
37         public string value { get; set; }
38     }
39     class Program
40     {
41         static void Main(string[] args)
42         {
43             Console.WriteLine("+++++开始+++++");
44             try
45             {
46                 using (StreamReader sr = new StreamReader(@"D:\My CShirp soft\ConsoleApplication1\ConsoleApplication1\ProductPara.xml"))
47                 {
48                     XmlSerializer xmldes = new XmlSerializer(typeof(PRODUCT));
49                     var re = xmldes.Deserialize(sr);
50                     Console.WriteLine(re);
51                 }
52             }
53             catch (Exception ex)
54             {
55                 Console.WriteLine(ex.Message.ToString());
56             }
57             Console.WriteLine("+++++结束+++++");
58             Console.Read();
59         }
60     }
61 }

 

posted @ 2017-08-09 09:29  Aaxuan  阅读(1374)  评论(0编辑  收藏  举报