/// <summary> /// Return attribute value if attribute <paramref name="attrName"/> /// exist, on <paramref name="defaultValue"/> if not exist. /// </summary> /// <param name="elem"></param> /// <param name="attrName"></param> /// <param name="defaultValue"></param> /// <returns></returns> public static string GetAttr(this XmlElement elem, string attrName, string defaultValue = null) { Expect.IsNotNull(elem, nameof(elem)); Expect.IsNotBlank(attrName, nameof(attrName)); return(elem.HasAttribute(attrName) ? elem.Attributes[attrName].Value : defaultValue); }
/// <summary> /// Set attribute in fluent mode /// </summary> /// <param name="elem"></param> /// <param name="attrName"></param> /// <param name="value"></param> /// <returns></returns> public static XmlElement SetAttr(this XmlElement elem, string attrName, string value) { Expect.IsNotNull(elem, nameof(elem)); Expect.IsNotBlank(attrName, nameof(attrName)); elem.SetAttribute(attrName, value); return(elem); }
/// <summary> /// Create child element /// </summary> /// <param name="parent">Parent element</param> /// <param name="name">Name of child element</param> /// <param name="xmlns">Child namespace</param> /// <returns></returns> public static XmlElement AppendElement(this XmlElement parent, string name, string xmlns = null) { Expect.IsNotNull(parent, nameof(parent)); Expect.IsNotBlank(name, nameof(name)); var elem = (xmlns != null) ? parent.OwnerDocument.CreateElement(name, xmlns) : parent.OwnerDocument.CreateElement(name); parent.AppendChild(elem); return(elem); }
/// <summary> /// Append exception detail to log file. /// </summary> /// <param name="exp">Exception to log</param> /// <param name="logPath">Log file path. </param> public static void LogToFile(this Exception exp, string logPath) { Expect.IsNotNull(exp, nameof(exp)); Expect.IsNotBlank(logPath, nameof(logPath)); try { File.AppendAllText(logPath, exp.GetFullTrace(), Encoding.UTF8); } catch (Exception inner) { Debug.WriteLine(inner.Message); Debug.WriteLine(inner.StackTrace); } }