使用XML序列化序列化包含集合的类的问题

时间:2009-07-22 19:18:31

标签: c# xml-serialization

我将此xml片段作为xml文件的一部分,该文件将被反序列化为c#对象

<EmailConfiguration>
<SendTo>
    <Address>myEmailAdress@bah.com</Address>
    <Address>also send to</Address>
</SendTo>
<CC>
    <Address>CC</Address>
</CC>
<BCC>
    <Address>BCC</Address>
</BCC>
</EmailConfiguration>

请注意,SendToAddress是地址集合

<Address>myEmailAddress@bah.com</Address>
<Address>also send to</Address>

我的班级目前看起来像这样

public class SendTo
{
    public string Address
    {
        get;
        set;
    }
}

这适用于1个地址,如何更改类以便它可以处理多个地址。

我试过

public class SendTo
{
    public string[] Address
    {
        get;
        set;
    }
}

但是这样,当xml被反序列化为类时,就不会填充任何内容。

更新

试过......也没有成功......

public class SendTo
{
    public List<string> Address
    {
        get;
        set;
    }
}

这是整个班级

namespace myConfiguration
   {
public class myConfiguration
{
    private string myID;


    public string MyID
    {
        get { return myID; }
        set { myID = value; }
    }

    public Locale Locale
    {
        get;
        set;
    }

    public EmailConfiguration EmailConfiguration
    {
        get;
        set;
    }

}

public class Locale
{
    public string LocaleName
    {
        get;
        set;
    }
    public string LocaleCode
    {
        get;
        set;
    }
}

public class EmailConfiguration
{
    public SendTo SendTo
    {
        set;
        get;
    }
    public SendTo CC
    {
        set;
        get;
    }
    public SendTo BCC
    {
        set;
        get;
    }
}

public class SendTo
{
    public List<string> Address
    {
        get;
        set;
    }
}

}

这是我从...反序列化的XML。

 <?xml version="1.0" encoding="utf-8" ?>
 <MyConfiguration>
 <MyID>vpfaewb</MyID>
 <Locale>
    <LocaleName>cs-CZ</LocaleName>
 <LocaleCode>1029</LocaleCode>
 </Locale>
<EmailConfiguration>
<SendTo>
    <Address>my email address</Address>
    <Address>also send to</Address>
</SendTo>
<CC>
    <Address>CC</Address>
</CC>
<BCC>
    <Address>BCC</Address>
</BCC>
  </EmailConfiguration>
</MyConfiguration>

5 个答案:

答案 0 :(得分:1)

如果您通过Microsoft的xsd.exe工具运行测试XML文件两次,首先将XML转换为XSD(xsd.exe yourfile.xml - &gt; yourfile.xsd),然后生成C#类从可以反序列化该XML内容的XSD(xsd.exe /c yourfile.xsd - &gt; yourfile.cs)开始,你最终会得到类似这样的东西(长而不是很漂亮.....)

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.4016
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=2.0.50727.3038.
// 


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class EmailConfiguration {

    private object[] itemsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("BCC", typeof(EmailConfigurationBCC), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlElementAttribute("CC", typeof(EmailConfigurationCC), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlElementAttribute("SendTo", typeof(EmailConfigurationSendTo), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public object[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class EmailConfigurationBCC {

    private string addressField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string Address {
        get {
            return this.addressField;
        }
        set {
            this.addressField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class EmailConfigurationCC {

    private string addressField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string Address {
        get {
            return this.addressField;
        }
        set {
            this.addressField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class EmailConfigurationSendTo {

    private EmailConfigurationSendToAddress[] addressField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Address", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
    public EmailConfigurationSendToAddress[] Address {
        get {
            return this.addressField;
        }
        set {
            this.addressField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class EmailConfigurationSendToAddress {

    private string valueField;

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public string Value {
        get {
            return this.valueField;
        }
        set {
            this.valueField = value;
        }
    }
}

我认为这会起作用 - 它不好,但它会起作用。您应该能够将完整的XML文件转换为可序列化/可反序列化的C#类。试试吧!

马克

答案 1 :(得分:1)

试试这个:

[XmlArrayItem("Address", IsNullable=false)]
public string[] SendTo
{
    get
    {
        return this.sendToField;
    }
    set
    {
        this.sendToField = value;
    }
}

答案 2 :(得分:0)

使用列表&lt;地址&gt;采集;这是串行化

答案 3 :(得分:0)

可能不是最好用的类型,但ArrayList也可序列化,值得一试。

答案 4 :(得分:0)

查看Xml Serialization提示2,了解有关序列化List集合的详细信息。