取消form自動提交
阿新 • • 發佈:2020-12-23
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寫入檔案或者從檔案讀取位元組流
- 使用XmlWriter和XmlReader
讀:通過XmlReader從檔案中讀取資料,然後根據節點處理(也可以通過MemoryStream和File)
寫:通過XmlWriter將資料寫入到MemoryStream中,然後再通過File寫入到檔案
其中stream是自定義的檔案處理類
Write the attribute and value
- 使用XmlSerializer
xml格式如上圖所示
model定義如下[XmlRoot("IND900PT")] public class TestReportInfo { /// <summary> /// Initializes a new instance of the <see cref=" TestReportInfo"/> class. /// </summary> public TestReportInfo() { }
具體實現如下
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; }