XmlWriterExtensions.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (c) Craftwork Games. All rights reserved.
  2. // Licensed under the MIT license.
  3. // See LICENSE file in the project root for full license information.
  4. using System;
  5. using System.Xml;
  6. using Microsoft.Xna.Framework;
  7. namespace MonoGame.Extended.Serialization.Xml;
  8. /// <summary>
  9. /// Provides extension methods for <see cref="XmlWriter"/> to simplify writing XML attributes from strongly-typed values
  10. /// </summary>
  11. public static class XmlWriterExtensions
  12. {
  13. /// <summary>
  14. /// Writes an XML attribute from an <see langword="int"/> value.
  15. /// </summary>
  16. /// <param name="writer">The XML writer instance.</param>
  17. /// <param name="attributeName">The name of the attribute to write.</param>
  18. /// <param name="value">The <see langword="int"/> value to write as the attribute value.</param>
  19. /// <exception cref="InvalidOperationException">The writer state is not valid for this operation.</exception>
  20. /// <exception cref="ArgumentException">The attribute name is not valid.</exception>
  21. public static void WriteAttributeInt(this XmlWriter writer, string attributeName, int value)
  22. {
  23. writer.WriteAttributeString(attributeName, $"{value}");
  24. }
  25. /// <summary>
  26. /// Writes an XML attribute from a <see langword="float"/> value.
  27. /// </summary>
  28. /// <param name="writer">The XML writer instance.</param>
  29. /// <param name="attributeName">The name of the attribute to write.</param>
  30. /// <param name="value">The <see langword="float"/> value to write as the attribute value.</param>
  31. /// <exception cref="InvalidOperationException">The writer state is not valid for this operation.</exception>
  32. /// <exception cref="ArgumentException">The attribute name is not valid.</exception>
  33. public static void WriteAttributeFloat(this XmlWriter writer, string attributeName, float value)
  34. {
  35. writer.WriteAttributeString(attributeName, $"{value}");
  36. }
  37. /// <summary>
  38. /// Writes an XML attribute from a <see langword="bool"/> value.
  39. /// </summary>
  40. /// <param name="writer">The XML writer instance.</param>
  41. /// <param name="attributeName">The name of the attribute to write.</param>
  42. /// <param name="value">The <see langword="bool"/> value to write as the attribute value.</param>
  43. /// <exception cref="InvalidOperationException">The writer state is not valid for this operation.</exception>
  44. /// <exception cref="ArgumentException">The attribute name is not valid.</exception>
  45. public static void WriteAttributeBool(this XmlWriter writer, string attributeName, bool value)
  46. {
  47. writer.WriteAttributeString(attributeName, $"{value}");
  48. }
  49. /// <summary>
  50. /// Writes an XML attribute from a <see cref="Rectangle"/> value.
  51. /// </summary>
  52. /// <param name="writer">The XML writer instance.</param>
  53. /// <param name="attributeName">The name of the attribute to write.</param>
  54. /// <param name="value">The <see cref="Rectangle"/> value to write as the attribute value.</param>
  55. /// <exception cref="InvalidOperationException">The writer state is not valid for this operation.</exception>
  56. /// <exception cref="ArgumentException">The attribute name is not valid.</exception>
  57. public static void WriteAttributeRectangle(this XmlWriter writer, string attributeName, Rectangle value)
  58. {
  59. writer.WriteAttributeString(attributeName, $"{value.X},{value.Y},{value.Width},{value.Height}");
  60. }
  61. /// <summary>
  62. /// Writes an XML attribute from a <see cref="Vector2"/> value.
  63. /// </summary>
  64. /// <param name="writer">The XML writer instance.</param>
  65. /// <param name="attributeName">The name of the attribute to write.</param>
  66. /// <param name="value">The <see cref="Vector2"/> value to write as the attribute value.</param>
  67. /// <exception cref="InvalidOperationException">The writer state is not valid for this operation.</exception>
  68. /// <exception cref="ArgumentException">The attribute name is not valid.</exception>
  69. public static void WriteAttributeVector2(this XmlWriter writer, string attributeName, Vector2 value)
  70. {
  71. writer.WriteAttributeString(attributeName, $"{value.X},{value.Y}");
  72. }
  73. /// <summary>
  74. /// Writes an XML attribute from a <see cref="Vector3"/> value.
  75. /// </summary>
  76. /// <param name="writer">The XML writer instance.</param>
  77. /// <param name="attributeName">The name of the attribute to write.</param>
  78. /// <param name="value">The <see cref="Vector3"/> value to write as the attribute value.</param>
  79. /// <exception cref="InvalidOperationException">The writer state is not valid for this operation.</exception>
  80. /// <exception cref="ArgumentException">The attribute name is not valid.</exception>
  81. public static void WriteAttributeVector3(this XmlWriter writer, string attributeName, Vector3 value)
  82. {
  83. writer.WriteAttributeString(attributeName, $"{value.X},{value.Y},{value.Z}");
  84. }
  85. }