/// <summary> /// Returns the child XML Element with matching <paramref name="name"/> if one exists. /// If an element with that name does not exist <paramref name="mode"/> determines the action taken. /// <see cref="XmlLookupMode.CreateOrRead"/> will create and return a new (empty) XML Element if one does not exist. /// <see cref="XmlLookupMode.ReadRequired"/> will throw an exception if an element is not found. /// </summary> public static XElement GetElement([NotNull] this XContainer node, [NotNull] string name, XmlLookupMode mode = XmlLookupMode.ReadOptional) { if (node == null) { throw new ArgumentNullException(nameof(node)); } if (name == null) { throw new ArgumentNullException(nameof(name)); } var element = node.Element(name); if (element != null) { return(element); } if (mode == XmlLookupMode.CreateOrRead) { node.Add(element = new XElement(name)); return(element); } if (mode == XmlLookupMode.ReadRequired) { throw new OcException("Required XML Element [" + name + "] not found."); } return(null); }
/// <summary> /// Returns the child XML Attribute with matching <paramref name="name"/> if one exists. /// If an attribute with that name does not exist <paramref name="mode"/> determines the action taken. /// <see cref="XmlLookupMode.CreateOrRead"/> will create and return a new (empty) XML Attribute if one does not exist. /// <see cref="XmlLookupMode.ReadRequired"/> will throw an exception if an attribute is not found. /// </summary> public static XAttribute GetAttribute([NotNull] this XElement node, [NotNull] string name, XmlLookupMode mode = XmlLookupMode.ReadOptional) { if (node == null) { throw new ArgumentNullException(nameof(node)); } if (name == null) { throw new ArgumentNullException(nameof(name)); } var attribute = node.Attribute(name); if (attribute != null) { return(attribute); } if (mode == XmlLookupMode.CreateOrRead) { node.Add(attribute = new XAttribute(name, "")); return(attribute); } if (mode == XmlLookupMode.ReadRequired) { throw new OcException("Required XML Attribute [" + name + "] not found."); } return(null); }