在实现IXmlSerializable接口时处理字符串的空值

时间:2010-03-30 16:35:48

标签: c# string null ixmlserializable

我有以下类实现IXmlSerializable。在实现WriteXml()时,我需要处理此类的字符串成员可能为空值的情况。处理此问题的最佳方法是什么?

目前,我正在使用默认构造函数,其中所有字符串属性都初始化为空字符串值。这样,当调用WriteXml()时,字符串不会为空。

我可以这样做的另一种方法是在xml中写入每个字符串之前使用String.IsNullOrEmpty进行检查。

有关如何改进此代码的任何建议?

    public sealed class FaxSender : IXmlSerializable
    {
        #region Public Constants

        private const string DEFAULT_CLASS_NAME = "FaxSender";

        #endregion Public Constants

        #region Public Properties

        public string Name { get; set; }

        public string Organization { get; set; }

        public string PhoneNumber { get; set; }

        public string FaxNumber { get; set; }

        public string EmailAddress { get; set; }

        #endregion Public Properties

        #region Public Methods

        #region Constructors

        public FaxSender()
        {
            Name = String.Empty;
            Organization = String.Empty;
            PhoneNumber = String.Empty;
            FaxNumber = String.Empty;
            EmailAddress = String.Empty;
        }

        public FaxSender(
            string name, 
            string organization, 
            string phoneNumber, 
            string faxNumber, 
            string emailAddress)
        {
            Name = name;
            Organization = organization;
            PhoneNumber = phoneNumber;
            FaxNumber = faxNumber;
            EmailAddress = emailAddress;
        }

        #endregion Constructors

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            throw new NotImplementedException();
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            throw new NotImplementedException();
        }

        public void WriteXml(System.Xml.XmlWriter xmlWriter)
        {
            try
            {
                // <sender>
                xmlWriter.WriteStartElement("sender");

                // Write the name of the sender as an element.
                xmlWriter.WriteElementString(
                    "name", 
                    this.Name.ToString(CultureInfo.CurrentCulture));

                // Write the organization of the sender as an element.
                xmlWriter.WriteElementString(
                    "organization", 
                    this.Organization.ToString(CultureInfo.CurrentCulture));

                // Write the phone number of the sender as an element.
                xmlWriter.WriteElementString(
                    "phone_number", 
                    this.PhoneNumber.ToString(CultureInfo.CurrentCulture));

                // Write the fax number of the sender as an element.
                xmlWriter.WriteElementString(
                    "fax_number", 
                    this.FaxNumber.ToString(CultureInfo.CurrentCulture));

                // Write the email address of the sender as an element.
                xmlWriter.WriteElementString(
                    "email_address", 
                    this.EmailAddress.ToString(CultureInfo.CurrentCulture));

                // </sender>
                xmlWriter.WriteEndElement();

            }
            catch
            {
                // Rethrow any exceptions.
                throw;
            }
        }

        #endregion IXmlSerializable Members

        #endregion Public Methods
    }

1 个答案:

答案 0 :(得分:3)

我有一些建议:

1 - 仅实施IXmlSerializable作为最后的手段,而是通过属性控制XML序列化。它更简单,更容易维护。它还处理属性为空引用。

[XmlRoot("sender")]
public sealed class FaxSender
{
    [XmlElement("name")]
    public string Name { get; set; }
    [XmlElement("organization")]
    public string Organization { get; set; }
    [XmlElement("phone_number")]
    public string PhoneNumber { get; set; }
    [XmlElement("fax_number")]
    public string FaxNumber { get; set; }
    [XmlElement("email_address")]
    public string EmailAddress { get; set; }

    // Remaining code omitted
}

2 - 如果您要做的唯一事情是重新抛出每个异常,请不要将代码包装在try \ catch中;

3 - .NET中常量的指导原则是使用PascalCase,这将导致DefaultClassName而不是DEFAULT_CLASS_NAME