1. 程式人生 > 其它 >取消form自動提交

取消form自動提交

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;
            }