2011-05-09 13 views
0

null値を持つ要素がスキップされる(すべての文字列型)を、のは言わせて:私が持っているクラスからの完全なXML構造を生成 - 問題:私は問題:)</p> <p>私は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="[email protected]">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=""/> 

ヌル割り当てられた私の "空の" Configurationクラスのすべてのプロパティ(これは文字列なので自動的に行われました)はスキップされました。

私はString型でより多くの要素を持ち、それらのすべて(null値を持つ)は生成されたXMLでスキップされました。通常は大丈夫ですが、この場合は私のアプリケーションで使用するのに正しい完全な "XML構造"の例を生成したいと思います。

+1

単にデフォルト値で設定クラスを作成するのはなぜですか? nullに初期化するのではなく、値で初期化し、このシリアル化が正しく機能します。 – Tejs

+0

+1 Thx、単にベスト;) – binball

答えて

関連する問題