mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-15 04:35:00 +00:00
XML utils untangling, potential optimizations
This commit is contained in:
@@ -182,6 +182,27 @@ namespace ICD.Common.Utils.Xml
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the child element with the given name under the current element.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="element"></param>
|
||||
/// <returns></returns>
|
||||
public static IcdXmlReader GetChildElement(this IcdXmlReader extends, string element)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
try
|
||||
{
|
||||
return extends.GetChildElements(element).First();
|
||||
}
|
||||
catch (InvalidOperationException e)
|
||||
{
|
||||
throw new FormatException("No child element with name " + element, e);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the child elements for the current element.
|
||||
/// </summary>
|
||||
@@ -199,6 +220,24 @@ namespace ICD.Common.Utils.Xml
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the child elements for the current element.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="element"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<IcdXmlReader> GetChildElements(this IcdXmlReader extends, string element)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
foreach (IcdXmlReader output in extends.GetChildElementsAsString(element).Select(child => new IcdXmlReader(child)))
|
||||
{
|
||||
output.SkipToNextElement();
|
||||
yield return output;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the child elements for the current element.
|
||||
/// </summary>
|
||||
@@ -230,6 +269,57 @@ namespace ICD.Common.Utils.Xml
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the child elements for the current element.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="element"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static IEnumerable<string> GetChildElementsAsString(this IcdXmlReader extends, string element)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
// Step into the first child.
|
||||
extends.SkipToNextElement();
|
||||
|
||||
while (extends.NodeType == XmlNodeType.Element || extends.NodeType == XmlNodeType.Comment)
|
||||
{
|
||||
switch (extends.NodeType)
|
||||
{
|
||||
case XmlNodeType.Comment:
|
||||
extends.Skip();
|
||||
break;
|
||||
|
||||
case XmlNodeType.Element:
|
||||
string name = extends.Name;
|
||||
string output = extends.ReadOuterXml();
|
||||
if (name == element)
|
||||
yield return output;
|
||||
break;
|
||||
}
|
||||
|
||||
extends.SkipInsignificantWhitespace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static bool TryGetChildElementAsString(this IcdXmlReader extends, string element, out string output)
|
||||
{
|
||||
output = null;
|
||||
|
||||
try
|
||||
{
|
||||
output = extends.GetChildElementsAsString(element).First();
|
||||
return true;
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Read Element Content
|
||||
|
||||
Reference in New Issue
Block a user