简介

     
之前举行了一个文件名称生成器,通过Webservice读取XML文件并拿那经过Json传至客户端挨的combobx,用户通过combobox选择而转文书的称谓模板,点击生成则会有一个文件名称并保存及数据库中。

事关到的编程内容

    webservice,XML,Access,Winform

Webservice的创建

      (1)创建空Web应用程序图片 1     (2)添加Web服务(asmx)

图片 2

    (3)在[WebMethod]下写要运用的章程

图片 3  
(4)通过浏览器查看是否创造好服务

图片 4  
(5)发现早已成立了该服务

图片 5Webservice的使用

  (1)创建一个新窗体,添加服务引用

图片 6 

   (2)点击“发现”找到相应的websevice,再点击“高级”

图片 7    (3)点击“添加Web引用”

图片 8   
(4)点击“此解决方案面临之Web服务”

图片 9    (5)点击“添加引用”

图片 10   (6)在窗体中援即可

图片 11XML文件之样式:

图片 12

数量库表的字段:

图片 13

Winform程序界面:

图片 14            
图片 15

Webservice调用类的艺术:

     
(1)在webservice中调用类的方式来完成某些操作,而非是一直写以webservice里。

图片 16

      (2)从XML文件读取信息(界面一中的combobox):

图片 17图片 18

 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)选取一个模板进行模糊查询,给新生成的文件名称加上个序列号:

图片 19图片 20

 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)读取一修信息并显示其详细字段:

图片 21图片 22

 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进行扦插信息操作:

图片 23图片 24

 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进行编辑信息操作:

图片 25图片 26

 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

末了效果:

图片 27图片 28图片 29

正文中针对片相对比主要之代码进行了招,希望对大家有些帮助。

 

相关文章

网站地图xml地图