perf: Avoid throwing exceptions in XmlReaderExtensions

This commit is contained in:
Chris Cameron
2018-10-19 16:25:57 -04:00
parent 78a3373592
commit 91f64a4fb1

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using ICD.Common.Properties; using ICD.Common.Properties;
using ICD.Common.Utils.EventArguments; using ICD.Common.Utils.EventArguments;
using ICD.Common.Utils.Extensions;
#if SIMPLSHARP #if SIMPLSHARP
using Crestron.SimplSharp.CrestronXml; using Crestron.SimplSharp.CrestronXml;
#else #else
@@ -193,14 +194,11 @@ namespace ICD.Common.Utils.Xml
if (extends == null) if (extends == null)
throw new ArgumentNullException("extends"); throw new ArgumentNullException("extends");
try IcdXmlReader output;
{ if (extends.GetChildElements(element).TryFirst(out output))
return extends.GetChildElements(element).First(); return output;
}
catch (InvalidOperationException e) throw new FormatException("No child element with name " + element);
{
throw new FormatException("No child element with name " + element, e);
}
} }
/// <summary> /// <summary>
@@ -304,20 +302,12 @@ namespace ICD.Common.Utils.Xml
} }
} }
public static bool TryGetChildElementAsString(this IcdXmlReader extends, string element, out string output) public static bool TryGetChildElementAsString(this IcdXmlReader extends, string element, out string output)
{ {
output = null; if (extends == null)
throw new ArgumentNullException("extends");
try return extends.GetChildElementsAsString(element).TryFirst(out output);
{
output = extends.GetChildElementsAsString(element).First();
return true;
}
catch (InvalidOperationException)
{
return false;
}
} }
#endregion #endregion