建立xml對應的對象類
根節點,對應類名
[XmlRoot("ComponentLog ")]
public class ComponentLog{
}
其餘節點,對應屬性名
[XmlElement("LogCategory")]
public string logCategory { get; set; }
也可以對應調集(若是同一節點有多個的話)
[XmlElement("LogContent")]
public ListlogContent { get; set; }
節點裏的內容
[XmlAttribute("Content")]
public string content { get; set; }
XML文件:
Sign
窗體中打開文件夾
FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
if (folderBrowser.ShowDialog() == DialogResult.OK)
{
txtFolderPath.Text = folderBrowser.SelectedPath;
}
窗體中跨線程調用組件(控件)
///文本框
///要顯示的內容
private void ShowText(TextBox textBox, String strText)
{
if (this.InvokeRequired)
{
this.Invoke((MethodInvoker)delegate () { ShowText(textBox, strText+"\r\n"); });
}
else
{
textBox.Text += DateTime.Now + " " + strText+"\r\n";
}
}
關閉窗口,退出全部進程
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
System.Environment.Exit(0);
}
將文本框的滾動條一貫處於最低端
private void txtReceive_TextChanged(object sender, EventArgs e)
{
txtReceive.SelectionStart = txtReceive.Text.Length;
txtReceive.ScrollToCaret();
}
銜接字符串
//str1不爲空,就將str1和「 」銜接
string journalString = str1 != string.Empty ? string.Concat(str1, " ") : string.Empty;
取得程序運行目錄下指定文件的途徑
string xmlPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "JournalLog\\123.xml");
獲取指定的編碼格局
Encoding gb2312 = Encoding.GetEncoding("GB2312");
依照指定編碼格局讀取文本內容
string strRead = File.ReadAllText(xmlPath,Encoding.Default);
依照指定編碼格局轉換已經讀取到的文本內容
//sendByte是字節,將其轉換成string
string strSendData = gb2312.GetString(sendByte);
或者string strSendData = Encoding.UTF8.GetString(sendByte);ui