XMLExtensions.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #region License
  2. // Copyright 2021 Kastellanos Nikolaos
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System.Globalization;
  17. using Microsoft.Xna.Framework;
  18. namespace System.Xml
  19. {
  20. public static class XMLExtensions
  21. {
  22. public static string GetAttribute(this XmlNode xmlNode, string attributeName)
  23. {
  24. XmlAttribute attribute = xmlNode.Attributes[attributeName];
  25. if (attribute == null) return null;
  26. return attribute.Value;
  27. }
  28. public static int? GetAttributeAsInt(this XmlNode xmlNode, string attributeName)
  29. {
  30. XmlAttribute attribute = xmlNode.Attributes[attributeName];
  31. if (attribute == null) return null;
  32. return Int32.Parse(attribute.Value, CultureInfo.InvariantCulture);
  33. }
  34. public static Color? GetAttributeAsColor(this XmlNode xmlNode, string attributeName)
  35. {
  36. XmlAttribute attribute = xmlNode.Attributes[attributeName];
  37. if (attribute == null) return null;
  38. attribute.Value = attribute.Value.TrimStart(new char[] { '#' });
  39. return new Color(
  40. Int32.Parse(attribute.Value.Substring(0, 2), System.Globalization.NumberStyles.HexNumber),
  41. Int32.Parse(attribute.Value.Substring(2, 2), System.Globalization.NumberStyles.HexNumber),
  42. Int32.Parse(attribute.Value.Substring(4, 2), System.Globalization.NumberStyles.HexNumber));
  43. }
  44. }
  45. }