zoukankan      html  css  js  c++  java
  • 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 softConsoleApplication1ConsoleApplication1ProductPara.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 }


    转载请标明出处

    作者:AaXuan

    地址:http://www.cnblogs.com/Aaxuan

    知识共享许可协议

    本作品采用  知识共享署名 3.0 未本地化版本许可协议  进行许可。

  • 相关阅读:
    boxcox1p归一化+pipeline+StackingCVRegressor
    rt-thread调度锁与关闭中断深度探究
    树莓派4最小化安装Linux
    树莓派4可以不用SD卡启动
    树莓派JTAG详细使用笔记
    树莓派上玩街机游戏
    用树莓派制作红白游戏机
    树莓派4上使用uboot+tftp调试rt-thread程序
    在window上搭建树莓派4b的RT-Thread开发环境2
    树莓派上运行RT-Thread并通过esp8266连接网络
  • 原文地址:https://www.cnblogs.com/Aaxuan/p/7323491.html
Copyright © 2011-2022 走看看