数据处理

Stream

在这之前先要说说.net中一个很重要的概念:stream

几乎所有的数据操作都离不开这个stream,stream的子类有很多,这里重点说下四种

  • Memory Stream: 争对内存操作的流
  • File Stream:争对文件操作的流
  • Bufferd Stream: 缓存流,通常跟其他流一起使用
  • NetWork Stream: 跟socket绑定使用的流

那么有流就会有流的读写,StreamReader和StreamWriter

这两者继承自TextReader和TextWriter

当然也可以是其他的比如XmlReader和XmlWriter

 有位大哥写的非常详细:https://www.cnblogs.com/crazytomato/p/8274803.html

xml两种读写方式

其实两者的差别只是xmlWriter(XmlReader)和XmlSerializer的区别,都会用到MemoryStream和File

读:通过File从文件读取字节流,然后通过ms能得到字节流,一般是byte的数组,通过File写入文件或者从文件读取字节流

  1. 使用XmlWriter和XmlReader
    读:通过XmlReader从文件中读取数据,然后根据节点处理(也可以通过MemoryStream和File)
    写:通过XmlWriter将数据写入到MemoryStream中,然后再通过File写入到文件

     其中stream是自定义的文件处理类

    Write the attribute and value

     

  2. 使用XmlSerializer

     xml格式如上图所示
    model定义如下

        [XmlRoot("IND900PT")]
        public class  TestReportInfo
        {
    
            /// <summary>
            /// Initializes a new instance of the <see cref=" TestReportInfo"/> class.
            /// </summary>
            public  TestReportInfo()
            {
            }
    
            /// <summary>
            /// Initializes a new instance of the <see cref=" TestReportInfo"/> class.
            /// </summary>
            /// <param name="startTime">The start time</param>
            /// <param name="endTime">The end time</param>
            /// <param name="interfaceInfos">The interface infos</param>
            /// <param name="applicationInfos">The component info</param>
            /// <param name="commandInfos">The command infos</param>
            public  TestReportInfo(
                string startTime, string endTime, List< TestInterfaceInfo> interfaceInfos,
                List< TestApplicationInfo> applicationInfos, List< TestCommandInfo> commandInfos)
            {
                StartTime = startTime;
                EndTime = endTime;
                Interfaces = interfaceInfos;
                Applications = applicationInfos;
                Commands = commandInfos;
            }
    
            /// <summary>
            /// Gets or sets start time
            /// </summary>
            public string StartTime { get; set; }
    
            /// <summary>
            /// Gets or sets end time
            /// </summary>
            public string EndTime { get; set; }
    
            /// <summary>
            /// Gets or sets interface models
            /// </summary>
            [XmlArrayItem("Interface")]
            public List< TestInterfaceInfo> Interfaces { get; set; }
    
            /// <summary>
            /// Gets or sets applications model
            /// </summary>
            [XmlArrayItem("Application")]
            public List< TestApplicationInfo> Applications { get; set; }
    
            /// <summary>
            /// Gets or sets command models
            /// </summary>
            [XmlElement("Command")]
            public List< TestCommandInfo> Commands { get; set; }
    
        }
    
    public class  TestInterfaceInfo
        {
            /// <summary>
            /// Initializes a new instance of the <see cref=" TestInterfaceInfo"/> class.
            /// </summary>
            public  TestInterfaceInfo()
            {
            }
    
            /// <summary>
            /// Initializes a new instance of the <see cref=" TestInterfaceInfo"/> class.
            /// </summary>
            /// <param name="location">The location</param>
            /// <param name="type">The type</param>
            /// <param name="version">The version</param>
            public  TestInterfaceInfo(string location, string type, string version)
            {
                Location = location;
                Type = type;
                Version = version;
            }
    
            /// <summary>
            /// Initializes a new instance of the <see cref=" TestInterfaceInfo"/> class.
            /// </summary>
            /// <param name="iInterface">The interface</param>
            public  TestInterfaceInfo(IInterface iInterface)
            {
                Location = iInterface.LogicalPort.ToString();
                Type = iInterface.Hardware;
                Version = iInterface.SoftwareVersion;
            }
    
            /// <summary>
            /// Gets or sets location
            /// </summary>
            [XmlAttribute("Location")]
            public string Location { get; set; }
    
            /// <summary>
            /// Gets or sets type
            /// </summary>
            [XmlAttribute("Type")]
            public string Type { get; set; }
    
            /// <summary>
            /// Gets or sets version
            /// </summary>
            [XmlAttribute("Version")]
            public string Version { get; set; }
            
        }
    
    public class  TestApplicationInfo
        {
            /// <summary>
            /// Initializes a new instance of the <see cref=" TestApplicationInfo"/> class.
            /// </summary>
            public  TestApplicationInfo()
            {
            }
    
            /// <summary>
            /// Initializes a new instance of the <see cref=" TestApplicationInfo"/> class.
            /// </summary>
            /// <param name="name">The name</param>
            /// <param name="version">The version</param>
            public  TestApplicationInfo(string name, string version)
            {
                Name = name;
                Version = version;
            }
    
            /// <summary>
            /// Initializes a new instance of the <see cref=" TestApplicationInfo"/> class.
            /// </summary>
            /// <param name="applicationVersionProvider">The applicationVersionProvider</param>
            public  TestApplicationInfo(IApplicationVersionProvider applicationVersionProvider)
            {
                Name = applicationVersionProvider.Name;
                Version = applicationVersionProvider.Version.ToString();
            }
    
            /// <summary>
            /// Gets or sets name
            /// </summary>
            [XmlAttribute(nameof(Name))]
            public string Name { get; set; }
    
            /// <summary>
            /// Gets or sets version
            /// </summary>
            [XmlAttribute(nameof(Version))]
            public string Version { get; set; }
        }
    
    
    public class  TestCommandInfo
        {
            /// <summary>
            /// Initializes a new instance of the <see cref=" TestCommandInfo"/> class.
            /// </summary>
            public  TestCommandInfo()
            {
            }
    
            /// <summary>
            /// Initializes a new instance of the <see cref=" TestCommandInfo"/> class.
            /// </summary>
            /// <param name="data">The received message</param>
            /// <param name="result">The result</param>
            /// <param name="info">The sent message</param>
            public  TestCommandInfo(string data, string result, string info)
            {
                Data = data;
                Result = result;
                Info = info;
            }
    
            /// <summary>
            /// Initializes a new instance of the <see cref=" TestCommandInfo"/> class.
            /// </summary>
            /// <param name="result">The  TestCommandResult</param>
            public  TestCommandInfo( TestCommandResult result)
            {
                Data = result.ReceivedMessage;
                Result = result.Result.ToString();
                Info = result.ResponseMessage;
            }
    
            /// <summary>
            /// Gets or sets the received message
            /// </summary>
            public string Data { get; set; }
    
            /// <summary>
            /// Gets or sets result
            /// </summary>
            public string Result { get; set; }
    
            /// <summary>
            /// Gets or sets the sent message
            /// </summary>
            public string Info { get; set; }
        }

    具体实现如下

    public void WriteDataToXmlAsync(TestReportInfo report)
            {
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    _serializer.Serialize(memoryStream, report);
                    File.WriteAllBytes(_file, data);
                }
            }
    
    public TestReportInfo LoadDataFromXmlAsync(IDataStream dataStream)
            {
                TestReportInfo reportInfo;
                var buffer = File.ReadAllBytes(_file);
                using (MemoryStream memoryStream = new MemoryStream(buffer))
                {
                    reportInfo =_serializer.Deserialize(memoryStream) as TestReportInfo;
                }
    
                return reportInfo;
            }


原文地址:https://www.cnblogs.com/xiaojidanbai/p/14180796.html