diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a44c90..08db5e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added + - XML TryGetAttribute methods + +### Changed + - Performance improvements when working with xml attributes + +### Removed + - Removed IcdXmlAttribute ## [7.1.0] - 2018-11-08 ### Added diff --git a/ICD.Common.Utils.Tests/Extensions/XmlReaderExtensionsTest.cs b/ICD.Common.Utils.Tests/Extensions/XmlReaderExtensionsTest.cs index 0f296c2..07d5ea6 100644 --- a/ICD.Common.Utils.Tests/Extensions/XmlReaderExtensionsTest.cs +++ b/ICD.Common.Utils.Tests/Extensions/XmlReaderExtensionsTest.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using ICD.Common.Utils.Xml; using NUnit.Framework; @@ -49,10 +50,10 @@ namespace ICD.Common.Utils.Tests.Extensions reader.ReadToNextElement(); - IcdXmlAttribute[] attributes = reader.GetAttributes().ToArray(); + KeyValuePair[] attributes = reader.GetAttributes().ToArray(); Assert.AreEqual(2, attributes.Length); - Assert.AreEqual("attr1", attributes[0].Name); - Assert.AreEqual("attr2", attributes[1].Name); + Assert.AreEqual("attr1", attributes[0].Key); + Assert.AreEqual("attr2", attributes[1].Key); } [Test] diff --git a/ICD.Common.Utils.Tests/Xml/IcdXmlAttributeTest.cs b/ICD.Common.Utils.Tests/Xml/IcdXmlAttributeTest.cs deleted file mode 100644 index e3952bd..0000000 --- a/ICD.Common.Utils.Tests/Xml/IcdXmlAttributeTest.cs +++ /dev/null @@ -1,17 +0,0 @@ -using ICD.Common.Properties; -using ICD.Common.Utils.Xml; -using NUnit.Framework; - -namespace ICD.Common.Utils.Tests.Xml -{ - [TestFixture] - public sealed class IcdXmlAttributeTest - { - [Test, UsedImplicitly] - public void ValueAsIntTest() - { - IcdXmlAttribute attribute = new IcdXmlAttribute("test", "12"); - Assert.AreEqual("12", attribute.Value); - } - } -} \ No newline at end of file diff --git a/ICD.Common.Utils.Tests/Xml/XmlUtilsTest.cs b/ICD.Common.Utils.Tests/Xml/XmlUtilsTest.cs index 086e83e..8c1f9c9 100644 --- a/ICD.Common.Utils.Tests/Xml/XmlUtilsTest.cs +++ b/ICD.Common.Utils.Tests/Xml/XmlUtilsTest.cs @@ -56,12 +56,12 @@ namespace ICD.Common.Utils.Tests.Xml { reader.ReadToNextElement(); - IcdXmlAttribute[] attributes = reader.GetAttributes().ToArray(); + KeyValuePair[] attributes = reader.GetAttributes().ToArray(); Assert.AreEqual(2, attributes.Length); - Assert.AreEqual("attr1", attributes[0].Name); + Assert.AreEqual("attr1", attributes[0].Key); Assert.AreEqual("1", attributes[0].Value); - Assert.AreEqual("attr2", attributes[1].Name); + Assert.AreEqual("attr2", attributes[1].Key); Assert.AreEqual("2", attributes[1].Value); } } diff --git a/ICD.Common.Utils/Xml/IcdXmlAttribute.cs b/ICD.Common.Utils/Xml/IcdXmlAttribute.cs deleted file mode 100644 index bf57430..0000000 --- a/ICD.Common.Utils/Xml/IcdXmlAttribute.cs +++ /dev/null @@ -1,80 +0,0 @@ -using System; - -namespace ICD.Common.Utils.Xml -{ - /// - /// IcdXmlAttribute represents an attribute="value" pair from xml. - /// - public struct IcdXmlAttribute : IEquatable - { - private readonly string m_Name; - private readonly string m_Value; - - public string Name { get { return m_Name; } } - public string Value { get { return m_Value; } } - - /// - /// Constructor. - /// - /// - /// - public IcdXmlAttribute(string name, string value) - { - m_Name = name; - m_Value = value; - } - - /// - /// Implementing default equality. - /// - /// - /// - /// - public static bool operator ==(IcdXmlAttribute a1, IcdXmlAttribute a2) - { - return a1.Equals(a2); - } - - /// - /// Implementing default inequality. - /// - /// - /// - /// - public static bool operator !=(IcdXmlAttribute a1, IcdXmlAttribute a2) - { - return !a1.Equals(a2); - } - - public bool Equals(IcdXmlAttribute other) - { - return m_Name == other.m_Name && - m_Value == other.m_Value; - } - - /// - /// Returns true if this instance is equal to the given object. - /// - /// - /// - public override bool Equals(object other) - { - return other is IcdXmlAttribute && Equals((IcdXmlAttribute)other); - } - - /// - /// Gets the hashcode for this instance. - /// - /// - public override int GetHashCode() - { - unchecked - { - int hash = 17; - hash = hash * 23 + (m_Name == null ? 0 : m_Name.GetHashCode()); - hash = hash * 23 + (m_Value == null ? 0 : m_Value.GetHashCode()); - return hash; - } - } - } -} diff --git a/ICD.Common.Utils/Xml/XmlReaderExtensions.cs b/ICD.Common.Utils/Xml/XmlReaderExtensions.cs index fbddbbc..fcf1ae7 100644 --- a/ICD.Common.Utils/Xml/XmlReaderExtensions.cs +++ b/ICD.Common.Utils/Xml/XmlReaderExtensions.cs @@ -28,7 +28,21 @@ namespace ICD.Common.Utils.Xml if (extends == null) throw new ArgumentNullException("extends"); - return extends.GetAttribute(name) != null; + string unused; + return extends.TryGetAttribute(name, out unused); + } + + /// + /// Returns true if the attribute exists. + /// + /// + /// + /// + /// + [PublicAPI] + public static bool TryGetAttribute(this IcdXmlReader extends, string name, out string value) + { + return (value = extends.GetAttribute(name)) != null; } /// @@ -37,13 +51,13 @@ namespace ICD.Common.Utils.Xml /// /// [PublicAPI] - public static IEnumerable GetAttributes(this IcdXmlReader extends) + public static IEnumerable> GetAttributes(this IcdXmlReader extends) { if (extends == null) throw new ArgumentNullException("extends"); while (extends.MoveToNextAttribute()) - yield return new IcdXmlAttribute(extends.Name, extends.Value); + yield return new KeyValuePair(extends.Name, extends.Value); // Move back to element. extends.MoveToElement(); diff --git a/ICD.Common.Utils/Xml/XmlUtils.cs b/ICD.Common.Utils/Xml/XmlUtils.cs index d2413f8..fa28888 100644 --- a/ICD.Common.Utils/Xml/XmlUtils.cs +++ b/ICD.Common.Utils/Xml/XmlUtils.cs @@ -55,7 +55,7 @@ namespace ICD.Common.Utils.Xml /// /// [PublicAPI] - public static IEnumerable GetAttributes(string xml) + public static IEnumerable> GetAttributes(string xml) { using (IcdXmlReader reader = new IcdXmlReader(xml)) { @@ -71,13 +71,30 @@ namespace ICD.Common.Utils.Xml /// /// [PublicAPI] - public static IcdXmlAttribute GetAttribute(string xml, string name) + public static string GetAttribute(string xml, string name) { - IcdXmlAttribute output; - if (GetAttributes(xml).TryFirst(a => a.Name == name, out output)) - return output; + using (IcdXmlReader reader = new IcdXmlReader(xml)) + { + reader.ReadToNextElement(); + return reader.GetAttribute(name); + } + } - throw new FormatException(string.Format("No attribute with name {0}", name)); + /// + /// Convenience method for getting attribute by name. + /// + /// + /// + /// + /// + [PublicAPI] + public static bool TryGetAttribute(string xml, string name, out string value) + { + using (IcdXmlReader reader = new IcdXmlReader(xml)) + { + reader.ReadToNextElement(); + return reader.TryGetAttribute(name, out value); + } } ///