从类生成完整的xml结构 - 问题:跳过具有空值的元素(所有字符串类型)

时间:2011-05-09 15:08:41

标签: c# xml-serialization

我有一个问题:)

我有一个XML,让我们说:

<?xml version="1.0" encoding="utf-16"?>
<Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <SMTPserver port="25" user="aa" password="123" senderAddress="aa@aa.com">127.0.0.1</SMTPserver>
</Configuration>

我有序列化类(注意,这里只显示部分代码):

....
   /// <summary>
    /// SMTP server settings.
    /// </summary>
    [Serializable()]
    public class SMTPserver
    {
        /// <summary>
        /// Gets or sets the SMTP port.
        /// </summary>
        /// <value>
        /// The SMTP port.
        /// </value>
        [XmlAttribute("port")]
        public int Port
        { get; set; }

        /// <summary>
        /// Gets or sets the user if SMTP needs authentication.
        /// </summary>
        /// <value>
        /// The SMTP user.
        /// </value>
        [XmlAttribute("user")]
        public string User
        { get; set; }

        /// <summary>
        /// Gets or sets the password if SMTP needs authentication.
        /// </summary>
        /// <value>
        /// The SMTP user password.
        /// </value>
        [XmlAttribute("password")]
        public string Password
        { get; set; }

        /// <summary>
        /// Gets or sets the sender address. Email address which wil be set as Sender of the all emails.
        /// </summary>
        /// <value>
        /// The sender address.
        /// </value>
        [XmlAttribute("senderAddress")]
        public string SenderAddress
        { get; set; }

        /// <summary>
        /// Gets or sets the name of the SMTP server or IP address.
        /// </summary>
        /// <value>
        /// The name of the SMTP server or his IP address.
        /// </value>
        [XmlText]
        public string SMTPServerAddressOrName
        { get; set; }
    }
...

我有序列化/反序列化方法。要序列化我使用这个方法:

/// <summary>
/// Serializes the specified data into XML string.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data">The data.</param>
/// <returns>XML string</returns>
public static string Serialize<T>(T data)
{
    string xmlText = String.Empty;

    using (StringWriter XMLText = new StringWriter())
    {
        XmlSerializer xSerializer = new XmlSerializer(typeof(T));
        xSerializer.Serialize(XMLText, data);
        xmlText = XMLText.ToString();
    }

    return xmlText;
}

一般来说一切正常。 但是我想从我的类中生成空的(没有数据)完整的XML结构(将其用作用户的示例,可以使用哪些条目)。

我在尝试:

// Configuration is my Serialization class.
//    [Serializable()]
//    [XmlRoot()]
        Configuration cleanConfig = new Configuration();
        string generatedXml = SettingsManager.Serialize<Configuration>(cleanConfig);

结果是:

<?xml version="1.0" encoding="utf-16"?>
<Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <SMTPserver port="0"/>
</Configuration>

代替:

<SMTPserver port="0" user="" password="" senderAddress=""/>

我的“空”配置类的所有属性都被分配了null(这是自动完成的,因为这是字符串)。

我有更多使用String类型的元素,并且在生成的XML中跳过了所有元素(带有null值)。通常它很好,但在这种情况下,我想生成完整的“XML结构”示例,这对我的应用程序使用是正确的。

1 个答案:

答案 0 :(得分:0)

尝试使用本主题中建议的解决方案:Xml Serialization - Render Empty Element

应该可以做到这一点。