using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;
using System.Xml;
using System.Collections;
using System.Linq;
/// <summary>
///WSxml 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class WSxml : System.Web.Services.WebService {
public WSxml () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public string saveXml(string cName, string age, string mobile, string email, string ads)
{
DateTime dt = DateTime.Now;
string cdate = dt.ToString();
XmlDocument xmldoc = new XmlDocument();
string xmlFilePath = HttpContext.Current.Server.MapPath("~/data/content.xml");
xmldoc.Load(xmlFilePath);
//查找根节点
XmlNode chat = xmldoc.SelectSingleNode("chat");
//使用xpath表达式选择文档中所有的tp子节点
XmlNodeList typeNodeList = xmldoc.SelectNodes("/chat/tp");
string str = string.Empty;
//定义一个List泛型集合
List<string> mlist = new List<string>();
if (typeNodeList != null)
{
string mbl = string.Empty;
foreach (XmlNode tpNode in typeNodeList)
{
//通过Attributes获得属性名字为mobile的属性
mbl = tpNode.Attributes["mobile"].Value;
//循环将mobile加入List泛型集合里
mlist.Add(mbl);
}
}
//查找是否存在List
if (mlist.Contains(mobile))
{
return "您的手机号码已被注册,请勿重复注册";
}
//创建节点
XmlElement infor = xmldoc.CreateElement("tp");
infor.SetAttribute("cName", cName);
infor.SetAttribute("age", age);
infor.SetAttribute("mobile", mobile);
infor.SetAttribute("email", email);
infor.SetAttribute("ads", ads);
infor.SetAttribute("cdate", cdate);
//存入根节点,保存文件
chat.AppendChild(infor);
xmldoc.Save(xmlFilePath);
return "恭喜您注册成功!";
}
[WebMethod]
public string readxmlTable()
{
XmlDocument xmldoc = new XmlDocument();
string xmlFilePath = HttpContext.Current.Server.MapPath("~/data/content.xml");
xmldoc.Load(xmlFilePath);
//使用xpath表达式选择文档中所有的tp子节点
XmlNodeList typeNodeList = xmldoc.SelectNodes("/chat/tp");
string str = string.Empty;
//定义一个SortedDictionary泛型集合
SortedDictionary<string, string> sd = new SortedDictionary<string, string>();
if (typeNodeList != null)
{
string cName = string.Empty, age = string.Empty, mobile = string.Empty, email = string.Empty, ads = string.Empty, cdate = string.Empty;
foreach (XmlNode tpNode in typeNodeList)
{
//通过Attributes获得属性名字为cName的属性
cName = tpNode.Attributes["cName"].Value;
age = tpNode.Attributes["age"].Value;
mobile = tpNode.Attributes["mobile"].Value;
email = tpNode.Attributes["email"].Value;
ads = tpNode.Attributes["ads"].Value;
cdate = tpNode.Attributes["cdate"].Value;
sd.Add(cdate, cName + "," + age + "," + mobile + "," + email + "," + ads );
}
//foreach (KeyValuePair<string, string> item in sd) 为正序asc
//使用泛型集合和linq将时间类型倒序desc排列
foreach (KeyValuePair<string, string> item in sd.Reverse())
{
str += item.Value + "," + Convert.ToDateTime(item.Key).ToString("yyyy年MM月dd日HH时mm分") + "|";
}
}
return str.Substring(0, str.Length - 1);
}
}