using System.Xml;
namespace SabreTools.Library.IO
{
///
/// Additional methods for XmlTextWriter
///
public static class XmlTextWriterExtensions
{
///
/// Write an attribute, forcing empty if null
///
/// XmlTextWriter to write out with
/// Name of the element
/// Value to write in the element
public static void WriteRequiredAttributeString(this XmlTextWriter writer, string localName, string value)
{
writer.WriteAttributeString(localName, value ?? string.Empty);
}
///
/// Force writing separate open and start tags, even for empty elements
///
/// XmlTextWriter to write out with
/// Name of the element
/// Value to write in the element
public static void WriteRequiredElementString(this XmlTextWriter writer, string localName, string value)
{
writer.WriteStartElement(localName);
if (value == null)
writer.WriteRaw(string.Empty);
else
writer.WriteString(value);
writer.WriteFullEndElement();
}
///
/// Write an attribute, if the value is not null or empty
///
/// XmlTextWriter to write out with
/// Name of the attribute
/// Value to write in the attribute
public static void WriteOptionalAttributeString(this XmlTextWriter writer, string localName, string value)
{
if (!string.IsNullOrEmpty(value))
writer.WriteAttributeString(localName, value);
}
///
/// Write an element, if the value is not null or empty
///
/// XmlTextWriter to write out with
/// Name of the element
/// Value to write in the element
public static void WriteOptionalElementString(this XmlTextWriter writer, string localName, string value)
{
if (!string.IsNullOrEmpty(value))
writer.WriteElementString(localName, value);
}
}
}