简介
之前做过一个文件名称生成器,通过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;
}
(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 = ;
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;
}
(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 + ).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;
}
(5)对XML进行插入信息操作:
public bool insert(string s)
{
bool state = true;
int i = ;
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[][])
{
i++;
}
}
element.SetAttribute("number", i.ToString());
element.SetAttribute("type", one_document[][]);
foreach (List<string> list in one_document)
{
string fieldindex = list[];
string fieldtype = list[];
string fieldname = list[];
string fieldvalue = list[];
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;
}
(6)对XML进行编辑信息操作:
public bool EditDATA(string str_s)
{
bool flag = true;
int temp = ;
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[];
string typename = basicinfo[];
string preview = basicinfo[];
int temp_documentindex = Convert.ToInt32(basicinfo[]);
int int_documentindex = temp_documentindex + temp;
string documentindex = int_documentindex.ToString();
one_document.Remove(one_document[]);
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[];
string fieldtype = list[];
string fieldname = list[];
string fieldvalue = list[];
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 = ; i <= ; 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 - ]);
}
}
}
doc.Save(@"F:\Winform Project\Namer\FieldList.xml");
return flag;
}
最后效果:
本文中对部分相对比较重要的代码进行了交代,希望对大家有点帮助。