public static SMS_SeatUsage ToModel(string xml) { SMS_SeatUsage model = new SMS_SeatUsage(); if (string.IsNullOrEmpty(xml)) { return(null); } XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.LoadXml(xml); //查找根节点 XmlNode node = xmlDoc.SelectSingleNode("//Root"); model.SchoolNum = node.Attributes["SchoolNum"].Value; model.UploadDate = DateTime.Parse(node.Attributes["UploadDate"].Value); model.UserCount = int.Parse(node.Attributes["UserCount"].Value); model.SeatCount = int.Parse(node.Attributes["SeatCount"].Value); model.RushCardCount = int.Parse(node.Attributes["RushCardCount"].Value); node = xmlDoc.SelectSingleNode("//Root/SeatUsage"); model.SeatUeage.EnterOutVisitors = int.Parse(node.Attributes["EnterOutVisitors"].Value); //选座情况 node = xmlDoc.SelectSingleNode("//Root/SeatUsage/SelectSeat"); model.SeatUeage.SelectSeatCount = int.Parse(node.Attributes["SelectSeatCount"].Value); model.SeatUeage.SelectSeatCountByAdmin = int.Parse(node.Attributes["SelectSeatCountByAdmin"].Value); model.SeatUeage.ReselectSeatCount = int.Parse(node.Attributes["ReselectSeatCount"].Value); model.SeatUeage.WaitSeatCount = int.Parse(node.Attributes["WaitSeatCount"].Value); //预约情况 node = xmlDoc.SelectSingleNode("//Root/SeatUsage/BookingSeat"); model.SeatUeage.BookingCount = int.Parse(node.Attributes["BookingCount"].Value); model.SeatUeage.BookingCancelCount = int.Parse(node.Attributes["BookingCancelCount"].Value); model.SeatUeage.BookingConfirmCount = int.Parse(node.Attributes["BookingConfirmCount"].Value); model.SeatUeage.BookingOverTimeCount = int.Parse(node.Attributes["BookingOverTimeCount"].Value); //暂离情况 node = xmlDoc.SelectSingleNode("//Root/SeatUsage/ShortLeave"); model.SeatUeage.ShortLeaveCount = int.Parse(node.Attributes["ShortLeaveCount"].Value); model.SeatUeage.ShortLeaveCountByAdmin = int.Parse(node.Attributes["ShortLeaveCountByAdmin"].Value); model.SeatUeage.ShortLeaveCountByReader = int.Parse(node.Attributes["ShortLeaveCountByReader"].Value); model.SeatUeage.ShortLeaveCountByService = int.Parse(node.Attributes["ShortLeaveCountByService"].Value); model.SeatUeage.ShortLeaveCountByUser = int.Parse(node.Attributes["ShortLeaveCountByUser"].Value); //释放座位情况 node = xmlDoc.SelectSingleNode("//Root/SeatUsage/Leave"); model.SeatUeage.LeaveCountByAdmin = int.Parse(node.Attributes["LeaveCountByAdmin"].Value); model.SeatUeage.LeaveCountByService = int.Parse(node.Attributes["LeaveCountByService"].Value); model.SeatUeage.LeaveCountByUser = int.Parse(node.Attributes["LeaveCountByUser"].Value); //续时 node = xmlDoc.SelectSingleNode("//Root/SeatUsage/ContniueTime"); model.SeatUeage.ContniueTimeCount = int.Parse(node.Attributes["ContniueTimeCount"].Value); model.SeatUeage.ContniueTimeCountByAdmin = int.Parse(node.Attributes["ContniueTimeCountByAdmin"].Value); model.SeatUeage.ContniueTimeCountByService = int.Parse(node.Attributes["ContniueTimeCountByService"].Value); model.SeatUeage.ContniueTimeCountByUser = int.Parse(node.Attributes["ContniueTimeCountByUser"].Value); //设备使用情况 XmlNodeList nodeList = xmlDoc.SelectNodes("//Root/DeviceUsage/Device"); foreach (XmlNode nodeItem in nodeList) { DeviceUsageInfo device = new DeviceUsageInfo(); device.DeviceNum = nodeItem.Attributes["DeviceNum"].Value; device.DeviceName = nodeItem.Attributes["DeviceName"].Value; device.ContniueTimeCount = int.Parse(nodeItem.Attributes["ContniueTimeCount"].Value); device.LeaveCount = int.Parse(nodeItem.Attributes["LeaveCount"].Value); device.RushCardCount = int.Parse(nodeItem.Attributes["RushCardCount"].Value); device.SelectSeatCount = int.Parse(nodeItem.Attributes["SelectSeatCount"].Value); device.ShortLeaveCount = int.Parse(nodeItem.Attributes["ShortLeaveCount"].Value); device.BookingConfirmCount = int.Parse(nodeItem.Attributes["BookingConfirmCount"].Value); device.ComeBackCount = int.Parse(nodeItem.Attributes["ComeBackCount"].Value); model.DeviceUsage.Add(device.DeviceNum, device); } //黑名单 node = xmlDoc.SelectSingleNode("//Root/Blacklist"); model.BlackListRecords.BlackListCount = int.Parse(node.Attributes["BlackListCount"].Value); model.BlackListRecords.ViolationRecordsCount = int.Parse(node.Attributes["ViolationRecordsCount"].Value); //违规 nodeList = xmlDoc.SelectNodes("//Root/Blacklist/Violation"); foreach (XmlNode nodeItem in nodeList) { ViolationInfo violation = new ViolationInfo(); model.BlackListRecords.ViolationRecords[(SeatManage.EnumType.ViolationRecordsType)System.Enum.Parse(typeof(SeatManage.EnumType.ViolationRecordsType), nodeItem.Attributes["Type"].Value)].Count = int.Parse(nodeItem.Attributes["Count"].Value); } } catch { throw; } return(model); }
/// <summary> /// 转换成XML /// </summary> /// <returns></returns> public static string ToXml(SMS_SeatUsage model) { //TODO:转换成xml结构的算法 //创建一个xml对象 XmlDocument xmlDoc = new XmlDocument(); //创建开头 XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null); xmlDoc.AppendChild(dec); //创建根节点 XmlElement root = xmlDoc.CreateElement("Root"); root.SetAttribute("SchoolNum", model.SchoolNum); root.SetAttribute("UploadDate", model.UploadDate.ToShortDateString()); root.SetAttribute("UserCount", model.UserCount.ToString()); root.SetAttribute("SeatCount", model.SeatCount.ToString()); root.SetAttribute("RushCardCount", model.RushCardCount.ToString()); //座位使用情况节点 XmlElement SeatUsageNode = xmlDoc.CreateElement("SeatUsage"); SeatUsageNode.SetAttribute("EnterOutVisitors", model.SeatUeage.EnterOutVisitors.ToString()); //选座情况节点 XmlElement SelectSeatNode = xmlDoc.CreateElement("SelectSeat"); SelectSeatNode.SetAttribute("SelectSeatCount", model.SeatUeage.SelectSeatCount.ToString()); SelectSeatNode.SetAttribute("SelectSeatCountByAdmin", model.SeatUeage.SelectSeatCountByAdmin.ToString()); SelectSeatNode.SetAttribute("ReselectSeatCount", model.SeatUeage.ReselectSeatCount.ToString()); SelectSeatNode.SetAttribute("WaitSeatCount", model.SeatUeage.WaitSeatCount.ToString()); SeatUsageNode.AppendChild(SelectSeatNode); //预约情况节点 XmlElement BookingSeatNode = xmlDoc.CreateElement("BookingSeat"); BookingSeatNode.SetAttribute("BookingCount", model.SeatUeage.BookingCount.ToString()); BookingSeatNode.SetAttribute("BookingCancelCount", model.SeatUeage.BookingCancelCount.ToString()); BookingSeatNode.SetAttribute("BookingConfirmCount", model.SeatUeage.BookingConfirmCount.ToString()); BookingSeatNode.SetAttribute("BookingOverTimeCount", model.SeatUeage.BookingOverTimeCount.ToString()); SeatUsageNode.AppendChild(BookingSeatNode); //暂离情况节点 XmlElement ShortLeaveNode = xmlDoc.CreateElement("ShortLeave"); ShortLeaveNode.SetAttribute("ShortLeaveCount", model.SeatUeage.ShortLeaveCount.ToString()); ShortLeaveNode.SetAttribute("ShortLeaveCountByAdmin", model.SeatUeage.ShortLeaveCountByAdmin.ToString()); ShortLeaveNode.SetAttribute("ShortLeaveCountByReader", model.SeatUeage.ShortLeaveCountByReader.ToString()); ShortLeaveNode.SetAttribute("ShortLeaveCountByService", model.SeatUeage.ShortLeaveCountByService.ToString()); ShortLeaveNode.SetAttribute("ShortLeaveCountByUser", model.SeatUeage.ShortLeaveCountByUser.ToString()); SeatUsageNode.AppendChild(ShortLeaveNode); //释放座位情况节点 XmlElement LeaveNode = xmlDoc.CreateElement("Leave"); LeaveNode.SetAttribute("LeaveCountByAdmin", model.SeatUeage.LeaveCountByAdmin.ToString()); LeaveNode.SetAttribute("LeaveCountByService", model.SeatUeage.LeaveCountByService.ToString()); LeaveNode.SetAttribute("LeaveCountByUser", model.SeatUeage.LeaveCountByUser.ToString()); SeatUsageNode.AppendChild(LeaveNode); //续时 XmlElement ContniueTimeNode = xmlDoc.CreateElement("ContniueTime"); ContniueTimeNode.SetAttribute("ContniueTimeCount", model.SeatUeage.ContniueTimeCount.ToString()); ContniueTimeNode.SetAttribute("ContniueTimeCountByAdmin", model.SeatUeage.ContniueTimeCountByAdmin.ToString()); ContniueTimeNode.SetAttribute("ContniueTimeCountByService", model.SeatUeage.ContniueTimeCountByService.ToString()); ContniueTimeNode.SetAttribute("ContniueTimeCountByUser", model.SeatUeage.ContniueTimeCountByUser.ToString()); SeatUsageNode.AppendChild(ContniueTimeNode); //添加到根节点 root.AppendChild(SeatUsageNode); //设备使用情况节点 XmlElement DeviceUsageNode = xmlDoc.CreateElement("DeviceUsage"); foreach (KeyValuePair <string, DeviceUsageInfo> device in model.DeviceUsage) { XmlElement DeviceNode = xmlDoc.CreateElement("Device"); DeviceNode.SetAttribute("DeviceNum", device.Value.DeviceNum.ToString()); DeviceNode.SetAttribute("DeviceName", device.Value.DeviceName.ToString()); DeviceNode.SetAttribute("ContniueTimeCount", device.Value.ContniueTimeCount.ToString()); DeviceNode.SetAttribute("LeaveCount", device.Value.LeaveCount.ToString()); DeviceNode.SetAttribute("RushCardCount", device.Value.RushCardCount.ToString()); DeviceNode.SetAttribute("SelectSeatCount", device.Value.SelectSeatCount.ToString()); DeviceNode.SetAttribute("ShortLeaveCount", device.Value.ShortLeaveCount.ToString()); DeviceNode.SetAttribute("ComeBackCount", device.Value.ComeBackCount.ToString()); DeviceNode.SetAttribute("BookingConfirmCount", device.Value.BookingConfirmCount.ToString()); DeviceUsageNode.AppendChild(DeviceNode); } //添加到根节点 root.AppendChild(DeviceUsageNode); //黑名单 XmlElement BlacklistNode = xmlDoc.CreateElement("Blacklist"); BlacklistNode.SetAttribute("BlackListCount", model.BlackListRecords.BlackListCount.ToString()); BlacklistNode.SetAttribute("ViolationRecordsCount", model.BlackListRecords.ViolationRecordsCount.ToString()); foreach (KeyValuePair <SeatManage.EnumType.ViolationRecordsType, ViolationInfo> violation in model.BlackListRecords.ViolationRecords) { XmlElement ViolationNode = xmlDoc.CreateElement("Violation"); ViolationNode.SetAttribute("Type", violation.Value.Type.ToString()); ViolationNode.SetAttribute("Count", violation.Value.Count.ToString()); BlacklistNode.AppendChild(ViolationNode); } //添加到根节点 root.AppendChild(BlacklistNode); xmlDoc.AppendChild(root); return(xmlDoc.OuterXml); }