簡介 之前做過一個文件名稱生成器,通過Webservice讀取XML文件並將其通過Json傳到客戶端中的combobx,用戶通過combobox選擇要生成文件的名稱模板,點擊生成則會產生一個文件名稱並保存到資料庫中。 涉及到的編程內容 webservice,XML,Access,Winform We ...
簡介
之前做過一個文件名稱生成器,通過Webservice讀取XML文件並將其通過Json傳到客戶端中的combobx,用戶通過combobox選擇要生成文件的名稱模板,點擊生成則會產生一個文件名稱並保存到資料庫中。
涉及到的編程內容
webservice,XML,Access,Winform
Webservice的創建
(1)創建空Web應用程式 (2)添加Web服務(asmx)
(3)在[WebMethod]下寫要使用的方法
(4)通過瀏覽器查看是否創建好服務
(5)發現已經建立了該服務
Webservice的使用
(1)創建一個新窗體,添加服務引用
(2)點擊“發現”找到相應的websevice,再點擊“高級”
(3)點擊“添加Web引用”
(4)點擊“此解決方案中的Web服務”
(5)點擊“添加引用”
(6)在窗體中引用即可
XML文件的樣式:
資料庫表的欄位:
Winform程式界面:
Webservice調用類的方法:
(1)在webservice中調用類的方法來完成某些操作,而不是直接寫在webservice里。
(2)從XML文件讀取信息(界面一中的combobox):
public List<string> BackSchemaList() { List<string> name = new List<string>(); XmlDocument doc = new XmlDocument(); doc.Load(@"F:\Winform Project\Namer\FieldList.xml"); XmlNode xn = doc.SelectSingleNode("FieldList"); XmlNodeList xnl = xn.ChildNodes; foreach (XmlNode xn1 in xnl) { string fieldvalue = ""; XmlElement xe = (XmlElement)xn1; string listnumber = xe.GetAttribute("number").ToString(); string listtype = xe.GetAttribute("type").ToString(); XmlNodeList xnl0 = xe.ChildNodes; foreach (XmlNode xn2 in xnl0) { XmlElement xe1 = (XmlElement)xn2; fieldvalue += xe1.GetAttribute("Value").ToString(); } name.Add(fieldvalue); name.Add(listtype); } return name; }View Code
(3)選取一個模板進行模糊查詢,給新生成的文件名稱加上個序列號:
public string generatedata(string s1,string s2) { string str_provider = "Provider=Microsoft.Ace.OLEDB.12.0;"; string str_source = "Data Source=F:/Winform Project/Namer/Namer.accdb;Persist Security Info=False;"; //數據源路徑 string str_connection = str_provider + str_source; //連接字元串 int xuliehao = 1; OleDbConnection cnn; OleDbCommand cmd; OleDbDataReader datar; string str_sql1 = "SELECT name FROM T_name WHERE name Like '"+s1+"%';"; cnn = new OleDbConnection(str_connection); cmd = new OleDbCommand(str_sql1, cnn); cnn.Open(); datar = cmd.ExecuteReader(); while (datar.Read()) { if (datar["name"].ToString() == null) { break; } else { xuliehao++; } } cnn.Close(); string number = string.Format("{0:000}", xuliehao); s1 += number; string dt = DateTime.Now.ToString(); string str_sql2 = "insert into T_name (name,code,timeNow,type) values ('"+s1+"','"+number+"','"+dt+"','"+s2+"');"; cnn = new OleDbConnection(str_connection); cmd = new OleDbCommand(str_sql2, cnn); cnn.Open(); int i = cmd.ExecuteNonQuery(); cnn.Close(); string newname = s1; return newname; }View Code
(4)讀取一條信息並顯示其詳細欄位:
public List<List<string>> Backonelist(int int_index,string str_type) { List<List<string>> documentlist = new List<List<string>>(); XmlDocument doc = new XmlDocument(); doc.Load(@"F:\Winform Project\Namer\FieldList.xml"); XmlNode xn = doc.SelectSingleNode("FieldList"); XmlNodeList xnl = xn.ChildNodes; foreach (XmlNode xn1 in xnl) { XmlElement xe = (XmlElement)xn1; XmlNodeList xnl0 = xe.ChildNodes; if (xe.GetAttribute("number").ToString() == (int_index + 1).ToString() && xe.GetAttribute("type").ToString() == str_type) { foreach (XmlNode xn2 in xnl0) { List<string> field = new List<string>(); XmlElement xe1 = (XmlElement)xn2; field.Add(xe1.GetAttribute("Index").ToString()); field.Add(xe1.GetAttribute("Type").ToString()); field.Add(xe1.GetAttribute("Name").ToString()); field.Add(xe1.GetAttribute("Value").ToString()); documentlist.Add(field); } } } return documentlist; }View Code
(5)對XML進行插入信息操作:
public bool insert(string s) { bool state = true; int i = 1; List<List<string>> one_document = new List<List<string>>(); one_document = JsonConvert.DeserializeObject<List<List<string>>>(s); XmlDocument doc = new XmlDocument(); doc.Load(@"F:\Winform Project\Namer\FieldList.xml"); XmlNode xn = doc.SelectSingleNode("FieldList"); XmlElement element = doc.CreateElement("list"); foreach (XmlNode node in xn.ChildNodes) { XmlElement xe = (XmlElement)node; if (xe.GetAttribute("type").ToString() == one_document[0][0]) { i++; } } element.SetAttribute("number", i.ToString()); element.SetAttribute("type", one_document[0][0]); foreach (List<string> list in one_document) { string fieldindex = list[1]; string fieldtype = list[2]; string fieldname = list[3]; string fieldvalue = list[4]; XmlElement element2 = doc.CreateElement("Field"); element.AppendChild(element2); element2.SetAttribute("Index", fieldindex); element2.SetAttribute("Type", fieldtype); element2.SetAttribute("Name", fieldname); element2.SetAttribute("Value", fieldvalue); } xn.AppendChild(element); doc.Save(@"F:\Winform Project\Namer\FieldList.xml"); return state; }View Code
(6)對XML進行編輯信息操作:
public bool EditDATA(string str_s) { bool flag = true; int temp = 1; List<List<string>> one_document = new List<List<string>>(); one_document = JsonConvert.DeserializeObject<List<List<string>>>(str_s); List<string> basicinfo = new List<string>(); basicinfo=one_document[0]; string typename = basicinfo[0]; string preview = basicinfo[1]; int temp_documentindex = Convert.ToInt32(basicinfo[2]); int int_documentindex = temp_documentindex + temp; string documentindex = int_documentindex.ToString(); one_document.Remove(one_document[0]); XmlDocument doc = new XmlDocument(); doc.Load(@"F:\Winform Project\Namer\FieldList.xml"); XmlNode xn = doc.SelectSingleNode("//list[@ number='" + documentindex + "'][@ type='" + typename + "']"); XmlNodeList xnl = xn.ChildNodes; List<XmlNode> xmllist = new List<XmlNode>(); foreach (XmlNode item in xnl) { xmllist.Add(item); } foreach (XmlNode item in xmllist) { item.ParentNode.RemoveChild(item); } foreach (List<string> list in one_document) { string fieldindex = list[0]; string fieldtype = list[1]; string fieldname = list[2]; string fieldvalue = list[3]; XmlElement element2 = doc.CreateElement("Field"); xn.AppendChild(element2); element2.SetAttribute("Index", fieldindex); element2.SetAttribute("Type", fieldtype); element2.SetAttribute("Name", fieldname); element2.SetAttribute("Value", fieldvalue); if (element2.GetAttribute("Name").ToString() == "category") { XmlElement element3 = doc.CreateElement("CodeList"); element2.AppendChild(element3); for (int i = 1; i <= 4; i++) { string[] array = { "personal", "work", "play", "trans" }; XmlElement element4 = doc.CreateElement("CodeWord"); element3.AppendChild(element4); element4.SetAttribute("Code", i.ToString()); element4.SetAttribute("Description", array[i - 1]); } } } doc.Save(@"F:\Winform Project\Namer\FieldList.xml"); return flag; }View Code
最後效果:
本文中對部分相對比較重要的代碼進行了交代,希望對大家有點幫助。