/// <summary> /// 序列化一个对象 /// </summary> /// <param name="info"></param> /// <returns></returns> private string SerializeLogInfo(IMonitorCtrlRes info) { Type type = info.GetType(); XmlSerializer serializer = new XmlSerializer(type); TextWriter textWriter = new StringWriter(); serializer.Serialize(textWriter, info); textWriter.Flush(); string xml = textWriter.ToString(); textWriter.Close(); XmlDocument doc = new XmlDocument(); doc.PreserveWhitespace = true; doc.LoadXml(xml); doc = Clean(doc); //若字符串开头为回车符,则去掉 xml = doc.OuterXml; if (xml[0] == '\r' && xml[1] == '\n') { xml = xml.Substring(2, xml.Length - 2); } return(xml); }
/// <summary> /// 反序列化xml字符串为指定对象 /// </summary> /// <param name="xml"></param> /// <param name="info"></param> private bool DeSerializeLogInfo(string xml, ControlOperateType ctrlType, out IMonitorCtrlRes info) { try { Type type; if (ctrlType == ControlOperateType.DecreaseBright) { type = typeof(CtrlDecreaseBrightRes); } else if (ctrlType == ControlOperateType.RestoreBright) { type = typeof(CtrlRestoreBrightRes); } else if (ctrlType == ControlOperateType.PowerOff) { type = typeof(CtrlPowerOffRes); } else if (ctrlType == ControlOperateType.PowerOn) { type = typeof(CtrlPowerOnRes); } else { info = null; return(false); } XmlSerializer serializer = new XmlSerializer(type); TextReader reader = new StringReader(xml); if (ctrlType == ControlOperateType.DecreaseBright) { info = (CtrlDecreaseBrightRes)serializer.Deserialize(reader); } else if (ctrlType == ControlOperateType.RestoreBright) { info = (CtrlRestoreBrightRes)serializer.Deserialize(reader); } else if (ctrlType == ControlOperateType.PowerOff) { info = (CtrlPowerOffRes)serializer.Deserialize(reader); } else { info = (CtrlPowerOnRes)serializer.Deserialize(reader); } reader.Close(); return(true); } catch { info = null; return(false); } }
/// <summary> /// 载入所有日志信息 /// </summary> /// <param name="info"></param> /// <returns></returns> public bool LoadAllLogInfo(out List <IMonitorCtrlRes> infoList) { lock (this) { infoList = new List <IMonitorCtrlRes>(); _scaleIndex[0] = 0; int infoCnt = GetXmlScaleChildCount(0, _scaleIndex); IMonitorCtrlRes ctrlInfo = null; string infoValue; string ctrlInfoTypeStr = ""; ControlOperateType ctrlType; Object ctrlTypeObj = null; for (int i = 0; i < infoCnt; i++) { _scaleIndex[1] = i; if (!GetCommonNote(1, _scaleIndex, CTRLINFO_TYPE, out ctrlInfoTypeStr)) { return(false); } ctrlTypeObj = Enum.Parse(typeof(ControlOperateType), ctrlInfoTypeStr); if (Enum.IsDefined(typeof(ControlOperateType), ctrlTypeObj)) { ctrlType = (ControlOperateType)ctrlTypeObj; } else { return(false); } if (!GetXmlScaleSpecElementByIndex(0, _scaleIndex, i, out infoValue)) { return(false); } if (!DeSerializeLogInfo(infoValue, ctrlType, out ctrlInfo)) { return(false); } infoList.Add(ctrlInfo); } } return(true); }
/// <summary> /// 添加一个日志信息 /// </summary> /// <param name="info"></param> /// <returns></returns> public bool AddCtrlLogInfoInfo(IMonitorCtrlRes info) { lock (this) { _scaleIndex[0] = 0; string infoStr = SerializeLogInfo(info); string indentInfoString = DoIndentation(infoStr, 2 * 2); if (!AddXmlScaleElement(0, _scaleIndex, 1, new string[] { FIRST_NAME }, new string[] { indentInfoString })) { return(false); } int infoCnt = GetXmlScaleChildCount(0, _scaleIndex); _scaleIndex[1] = infoCnt - 1; if (!SetCommonNote(1, _scaleIndex, CTRLINFO_TYPE, info.OperateType.ToString())) { return(false); } return(true); } }
/// <summary> /// 序列化一个对象 /// </summary> /// <param name="info"></param> /// <returns></returns> private string SerializeLogInfo(IMonitorCtrlRes info) { Type type = info.GetType(); XmlSerializer serializer = new XmlSerializer(type); TextWriter textWriter = new StringWriter(); serializer.Serialize(textWriter, info); textWriter.Flush(); string xml = textWriter.ToString(); textWriter.Close(); XmlDocument doc = new XmlDocument(); doc.PreserveWhitespace = true; doc.LoadXml(xml); doc = Clean(doc); //若字符串开头为回车符,则去掉 xml = doc.OuterXml; if (xml[0] == '\r' && xml[1] == '\n') { xml = xml.Substring(2, xml.Length - 2); } return xml; }
/// <summary> /// 反序列化xml字符串为指定对象 /// </summary> /// <param name="xml"></param> /// <param name="info"></param> private bool DeSerializeLogInfo(string xml, ControlOperateType ctrlType, out IMonitorCtrlRes info) { try { Type type; if (ctrlType == ControlOperateType.DecreaseBright) { type = typeof(CtrlDecreaseBrightRes); } else if (ctrlType == ControlOperateType.RestoreBright) { type = typeof(CtrlRestoreBrightRes); } else if (ctrlType == ControlOperateType.PowerOff) { type = typeof(CtrlPowerOffRes); } else if (ctrlType == ControlOperateType.PowerOn) { type = typeof(CtrlPowerOnRes); } else { info = null; return false; } XmlSerializer serializer = new XmlSerializer(type); TextReader reader = new StringReader(xml); if (ctrlType == ControlOperateType.DecreaseBright) { info = (CtrlDecreaseBrightRes)serializer.Deserialize(reader); } else if (ctrlType == ControlOperateType.RestoreBright) { info = (CtrlRestoreBrightRes)serializer.Deserialize(reader); } else if (ctrlType == ControlOperateType.PowerOff) { info = (CtrlPowerOffRes)serializer.Deserialize(reader); } else { info = (CtrlPowerOnRes)serializer.Deserialize(reader); } reader.Close(); return true; } catch { info = null; return false; } }
/// <summary> /// 添加一个日志信息 /// </summary> /// <param name="info"></param> /// <returns></returns> public bool AddCtrlLogInfoInfo(IMonitorCtrlRes info) { lock (this) { _scaleIndex[0] = 0; string infoStr = SerializeLogInfo(info); string indentInfoString = DoIndentation(infoStr, 2 * 2); if (!AddXmlScaleElement(0, _scaleIndex, 1, new string[] { FIRST_NAME }, new string[] { indentInfoString })) { return false; } int infoCnt = GetXmlScaleChildCount(0, _scaleIndex); _scaleIndex[1] = infoCnt - 1; if (!SetCommonNote(1, _scaleIndex, CTRLINFO_TYPE, info.OperateType.ToString())) { return false; } return true; } }