public void MonitorStateSerialization() { MonitorState ms = new MonitorState(); ms.ExecutedOnHostComputer = "localhost"; ms.RawDetails = "This is a test"; ms.HtmlDetails = "<p>This is a test</p>"; ms.State = CollectorState.NotAvailable; ms.Timestamp = DateTime.Now; ms.StateChangedTime = DateTime.Now; ms.CallDurationMS = 1001; ms.AlertsRaised.Add("Test alert"); ms.ChildStates.Add(new MonitorState() { State = CollectorState.Good, RawDetails = "Child test" }); Assert.IsNotNull(ms.ToXml(), "MonitorState ToXml is null"); Assert.AreNotEqual("", ms.ToXml(), "MonitorState ToXml is empty"); string msSerialized = ms.ToXml(); ms.FromXml(ms.ToXml()); Assert.AreEqual(msSerialized, ms.ToXml(), "Serialized/Deserialized failed"); }
public void FromXml(string content) { XmlDocument xdoc = new XmlDocument(); xdoc.LoadXml(content); XmlElement root = xdoc.DocumentElement; UniqueId = root.ReadXmlElementAttr("uniqueId", Guid.NewGuid().ToString()); State = CollectorStateConverter.GetCollectorStateFromText(root.ReadXmlElementAttr("state", "NotAvailable")); try { StateChangedTime = DateTime.Parse(root.ReadXmlElementAttr("stateChangedTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); } catch { } try { Timestamp = DateTime.Parse(root.ReadXmlElementAttr("timeStamp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); } catch { } try { CallDurationMS = root.ReadXmlElementAttr("callDurationMS", 0); } catch { } ForAgent = root.ReadXmlElementAttr("forAgent", ""); ForAgentType = root.ReadXmlElementAttr("forAgentType", ""); ForAgentId = root.ReadXmlElementAttr("forAgentId", -1); CurrentValue = root.ReadXmlElementAttr("currentValue", ""); PrimaryUIValue = root.ReadXmlElementAttr("primaryUIValue", false); ExecutedOnHostComputer = root.ReadXmlElementAttr("executedOnHostComputer", ""); RanAs = root.ReadXmlElementAttr("ranAs", ""); RepeatCount = root.ReadXmlElementAttr("repeatCount", 0); try { AlertHeader = root.SelectSingleNode("alertHeader").InnerText; } catch { } try { AlertFooter = root.SelectSingleNode("alertFooter").InnerText; } catch { } try { AdditionalAlertText = root.SelectSingleNode("additionalAlertText").InnerText; } catch { } RawDetails = root.SelectSingleNode("rawDetails").InnerText; HtmlDetails = root.SelectSingleNode("htmlDetails").InnerText; XmlNodeList alertNodes = root.SelectNodes("alerts"); AlertsRaised = new List <string>(); foreach (XmlNode alertNode in alertNodes) { if (alertNode.InnerText.Trim().Length > 0) { AlertsRaised.Add(alertNode.InnerText); } } XmlNodeList scriptsRanNodes = root.SelectNodes("scriptsRan"); ScriptsRan = new List <string>(); foreach (XmlNode scriptNode in scriptsRanNodes) { if (scriptNode.InnerText.Trim().Length > 0) { ScriptsRan.Add(scriptNode.InnerText); } } ChildStates = new List <MonitorState>(); XmlNodeList childStates = root.SelectNodes("childStates/monitorState"); foreach (XmlNode childStateNode in childStates) { MonitorState childState = new MonitorState(); childState.FromXml(childStateNode.OuterXml); ChildStates.Add(childState); } }