XML 以字串傳輸,為方便操作建立強型別模型。先定義 FileRequestMode 列舉區分傳送/接收:
public enum FileRequestMode {
Send = 0,
Receive
}
再定義 FileProtocol 結構,提供協議屬性的強型別存取,並覆寫 ToString() 自動產生 XML,客戶端只需呼叫 ToString() 即可:
public struct FileProtocol {
private readonly FileRequestMode mode;
private readonly int port;
private readonly string fileName;
public FileProtocol(FileRequestMode mode, int port, string fileName) {
this.mode = mode; this.port = port; this.fileName = fileName;
}
public FileRequestMode Mode { get { return mode; } }
public int Port { get { return port; } }
public string FileName { get { return fileName; } }
public override string ToString() {
return String.Format("<protocol><file name=\"{0}\" mode=\"{1}\" port=\"{2}\" /></protocol>",
fileName, mode, port);
}
}